Django: Как мне проверить unique_together внутри модели - PullRequest
2 голосов
/ 17 декабря 2009

У меня есть следующее:

class AccountAdmin(models.Model):

    account = models.ForeignKey(Account)
    is_master = models.BooleanField()
    name = models.CharField(max_length=255)
    email = models.EmailField()

    class Meta:
        unique_together = (('Account', 'is_master'), ('Account', 'username'),)

Если я затем создаю новый AccountAdmin с тем же именем пользователя, что и у другого пользователя в той же учетной записи, вместо того, чтобы выдавать ошибку при отображении в шаблоне, он разрывается с помощью IntegrityError, и страница умирает. Я хотел бы, чтобы, на мой взгляд, я мог просто пойти:

if new_accountadmin_form.is_valid():
    new_accountadmin_form.save()

Как мне победить эту проблему. Существует ли второй is_valid() тип метода, который проверяет БД на нарушение части unique_together = (('Account', 'is_master'), ('Account', 'username'),)?

Мне бы хотелось, чтобы мне не приходилось ловить ошибку IntegrityError. Это доменная логика, смешанная с логикой представления. Это нарушает СУХОЙ, потому что, если я отображу ту же форму на 2 страницах, мне придется повторить тот же блок. Это также нарушает DRY, потому что если у меня есть две формы для одной и той же вещи, я должен написать то же самое, за исключением: снова.

Ответы [ 3 ]

7 голосов
/ 17 декабря 2009

Есть два варианта:

a) Есть блок try, где вы сохраняете свою модель, захватываете ошибку IntegrityError и работаете с ней. Что-то вроде:

try:
    new_accountadmin_form.save()
except IntegrityError:
    new_accountadmin_form._errors["account"] = ["some message"]
    new_accountadmin_form._errors["is_master"] = ["some message"]

    del new_accountadmin_form.cleaned_data["account"]
    del new_accountadmin_form.cleaned_data["is_master"]

b) В методе clean () вашей формы проверьте, существует ли строка, и выведите forms.ValidationError с соответствующим сообщением. Пример здесь .


Итак, б) это ... Вот почему я ссылался на документацию; все, что вам нужно, там.

Но это было бы что-то вроде:

class YouForm(forms.Form):
    # Everything as before.
    ...

    def clean(self):
       """ This is the form's clean method, not a particular field's clean method """
       cleaned_data = self.cleaned_data

       account = cleaned_data.get("account")
       is_master = cleaned_data.get("is_master")
       username = cleaned_data.get("username")

       if AccountAdmin.objects.filter(account=account, is_master=is_master).count() > 0:
           del cleaned_data["account"]
           del cleaned_data["is_master"]
           raise forms.ValidationError("Account and is_master combination already exists.")

       if AccountAdmin.objects.filter(account=account, username=username).count() > 0:
           del cleaned_data["account"]
           del cleaned_data["username"]
           raise forms.ValidationError("Account and username combination already exists.")

    # Always return the full collection of cleaned data.
    return cleaned_data

Для чего это стоит - я только что понял, что ваш уникальный_все выше ссылается на поле с именем пользователя, которое не представлено в модели.

Приведенный выше метод очистки вызывается после вызова всех методов очистки для отдельных полей.

2 голосов
/ 04 января 2013

И для совершенно общего способа. В модели есть следующие два вспомогательных фнс:

def getField(self,fieldName):
  # return the actual field (not the db representation of the field)
  try:
    return self._meta.get_field_by_name(fieldName)[0]
  except models.fields.FieldDoesNotExist:
    return None

и

def getUniqueTogether(self):
  # returns the set of fields (their names) that must be unique_together
  # otherwise returns None
  unique_together = self._meta.unique_together
  for field_set in unique_together:
    return field_set
  return None

А в форме есть следующие фн:

def clean(self):
  cleaned_data = self.cleaned_data
  instance = self.instance

  # work out which fields are unique_together
  unique_filter = {}
  unique_fields = instance.getUniqueTogether()
  if unique_fields:
    for unique_field in unique_fields:
      field = instance.getField(unique_field)
      if field.editable: 
        # this field shows up in the form,
        # so get the value from the form
        unique_filter[unique_field] = cleaned_data[unique_field]
      else: 
        # this field is excluded from the form,
        # so get the value from the model
        unique_filter[unique_field] = getattr(instance,unique_field)

    # try to find if any models already exist in the db;
    # I find all models and then exlude those matching the current model.
    existing_instances = type(instance).objects.filter(**unique_filter).exclude(pk=instance.pk)

    if existing_instances:
      # if we've gotten to this point, 
      # then there is a pre-existing model matching the unique filter
      # so record the relevant errors
      for unique_field in unique_fields:
        self.errors[unique_field] = "This value must be unique."
1 голос
/ 17 декабря 2009

Model.Meta.unique_together создает ограничение, ограниченное базой данных, в то время как ModelForm.is_valid () в основном основан на правильных типах. Событие, если оно проверяет ограничения, у вас будет состояние гонки, которое может вызвать ошибку IntegrityError в вызове save ().

Возможно, вы хотите перехватить IntegrityError:

if new_accountadmin_form.is_valid():
    try:
        newaccountadmin_form.save()
    except IntegrityError, error:
        # here's your error handling code
...