#31774: validate_unique in Model class doesn't validate uniqueness for 
ImageField.
-------------------------------------+-------------------------------------
               Reporter:  gghildyal  |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 '''Steps for reproduction:'''

 * Consider a model with an ImageField

 {{{#!python
 @final
 class ValidateUnique(models.Model):
     logo = models.ImageField(
           _('Official Logo'),
           help_text=_('The official logo..'),
           upload_to='logos/',
           blank=True,
           unique=True,
     )
  }}}

 * Integrate the above model with the admin interface.
 * Create two instances of the model via the admin interface.
 * Upload an image for a logo in the first one.
 * Upload the same image for a logo in the second one.

 '''Expected Result:''' The 'logo' field in the form shows an error saying
 the logo already exists (Fails uniqueness constraint)

 '''Actual Result:''' An error page with a stacktrace (running with
 DEBUG=True) throwing an IntegrityError.

 '''Possible cause''':

 In the Model class in django/db/models/base.py

 {{{#!python

 def _perform_unique_checks(self, unique_checks):
   ...
   ...
            # some fields were skipped, no reason to do the check
             if len(unique_check) != len(lookup_kwargs):
                 continue

             qs = model_class._default_manager.filter(**lookup_kwargs)
  ....
 }}}

 In the above code, the lookup_kwargs gets this value

 {{{  lookup_kwargs = {'logo': <ImageFieldFile: temp.logo.png>}  }}}

 The queryset fails to return the first model instance because the lookup
 for ImageField fails. So validation error isn't added to the form and
 instead IntegrityError exception is raised.

 //It would work if the lookup value was the the fields 'name' attribute,
 say if the lookup_kwargs looked like this: //

 {{{ lookup_kwargs = {'logo': '/logos/temp.logo.png/'} }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31774>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.181d3383a7b8c149731640d2187048c3%40djangoproject.com.

Reply via email to