2011-12-08 2 views
0

unique_together 제약 조건이있는 모델이 있습니다. 나는 충돌의 포스트 - 그것, 제약 체크 새로운, 그리고의 경우를 작성하는 양식을 사용하는 경우unique_together 유효성 검증 오류를 차별화하십시오.

class Postit(models.Model): 
    """Represents a single post-it.""" 
    x_axis = models.PositiveIntegerField(_('X axis')) 
    y_axis = models.PositiveIntegerField(_('Y axis')) 
    content = models.CharField(_('Content'), max_length=140, default='') 

    class Meta: 
     unique_together = ('x_axis', 'y_axis') 

오류가 non_field_errors 속성에 나열됩니다. 벌금.

제 문제는 양식 오류의 종류에 따라 다른 조치를 실행하고 싶습니다. 고유 한 제약 조건 오류가있는 경우 특정 작업을 원하고 다른 종류의 오류에 대해서는 다른 작업을 원합니다.

내 앱이 여러 언어로 번역 될 경우 제약 조건 또는 다른 이유로 양식이 유효하지 않은지 어떻게 알 수 있습니까?

답변

0

non_field_errors 만 확인하려는 경우 비교중인 문자열을 번역 할 수 있습니다.

from django.utils.translation import ugettext_lazy as _ 
if _('Some error text') in self._errors['__all__']: 
    # do something 

실제로 가장 우아한 해결책은 아닙니다. 가장 좋은 방법은 Model.clean 또는 Model.validate_unique에서 직접 unique_together 제약 조건을 확인하고 그에 따라 응답하는 것입니다.

관련 문제