﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31774	validate_unique in Model class doesn't validate uniqueness for ImageField.	Gaurav Ghildyal	nobody	"'''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/'} }}}"	New feature	closed	Database layer (models, ORM)	3.0	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
