Ticket #9039: unqieu-validate-nulls.2.diff

File unqieu-validate-nulls.2.diff, 1.8 KB (added by Alex Gaynor, 16 years ago)

Ok this should work

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 6024a1c..0c5a2c1 100644
    a b class BaseModelForm(BaseForm):  
    246246            lookup_kwargs = {}
    247247            for field_name in unique_check:
    248248                lookup_kwargs[field_name] = self.cleaned_data[field_name]
    249            
     249                # If the field can be null, we need to exclude null values from
     250                # the query, since in SQL NULL != NULL
     251                if self.instance._meta.get_field_by_name(field_name)[0].null:
     252                    lookup_kwargs["%s__isnull" % field_name] = False
    250253            qs = self.instance.__class__._default_manager.filter(**lookup_kwargs)
    251 
     254           
    252255            # Exclude the current object from the query if we are editing an
    253256            # instance (as opposed to creating a new one)
    254257            if self.instance.pk is not None:
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index e3821de..d9195e3 100644
    a b class Inventory(models.Model):  
    144144
    145145   def __unicode__(self):
    146146      return self.name
     147
     148class Patient(models.Model):
     149    special_id = models.IntegerField(blank=True, null=True, unique=True)
    147150     
    148151__test__ = {'API_TESTS': """
    149152>>> from django import forms
    ValidationError: [u'Select a valid choice. z is not one of the available choices  
    12511254>>> core = form.save()
    12521255>>> core.parent
    12531256<Inventory: Pear>
     1257
     1258>>> class PatientForm(ModelForm):
     1259...     class Meta:
     1260...        model = Patient
     1261>>> form = PatientForm({})
     1262>>> form.save()
     1263<Patient: Patient object>
     1264>>> form = PatientForm({})
     1265>>> form.save()
     1266<Patient: Patient object>
    12541267"""}
Back to Top