Code

Opened 8 years ago

Closed 7 years ago

#2019 closed defect (duplicate)

manipulator_validator_unique_together does not work correctly

Reported by: imunitic@… Owned by: adrian
Component: contrib.admin Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Hello,my name is Ivica Munitic and i come from Croatia.
English is not my primary language so sorry if a don't spell some words correctly :)

I've been working on some small application and I think i found a bug.
The problem is with the manipulator_validator_unique_together function.
My model class where the problem occurres looks like this:

class Criteria(models.Model):
    subject = models.ForeignKey(Subject)
    professor = models.ForeignKey(Professor)
    activity_type = models.ForeignKey(ActivityType)
    coefficient = models.FloatField(max_digits=3, decimal_places=2, validator_list=[range_validator])
    minresult = models.FloatField('Minimal result', max_digits=3, decimal_places=2, validator_list=[range_validator])
    created = models.DateTimeField('created', auto_now_add=True)

    def __str__(self):
        return "%s, %s, %s" % (self.subject.name, self.professor.get_fullname(), self.activity_type.name)
    
    class Meta:
        unique_together = (('subject', 'professor', 'activity_type'),)
        
    class Admin:
        date_hierarchy = 'created'
        list_display = ('subject', 'professor', 'activity_type', 'created')
        list_filter = ('created', 'activity_type' )
        ordering = ['-created']

As you can see I added a unique_together field.
I the admin site when i try to save a duplicate i get an exception not a ValidationError as expected.
I debugged a bit and found the problem to be in this piece of code (django/db/models/manipulators.py:286):

        field_val = all_data.get(f.attname, None)
        if field_val is None:
            # This will be caught by another validator, assuming the field
            # doesn't have blank=True.
            return

f.attname for the self.professor field is professor_id but all_data dictionary has a professor not a professor_id .
manipulator_validator_unique_together just returns and does not raise a ValidationError .
If I change f.attname to f.name then it works but i don't know if this is correct.

So, this is my first ticket :)
I hope it is a good one :))

Attachments (0)

Change History (8)

comment:1 Changed 8 years ago by tom@…

I'm also having problems with unique_together (SVN revision 3116)

When I try to save a duplicate in the admin interface using SQLite as the database, I get:

OperationalError (columns first_id, second_id are not unique)

When using MySQL as the database, I get:

IntegrityError (1062, "Duplicate entry '1-2' for key 2")

comment:2 Changed 8 years ago by tom@…

The described workaround works for me, but I don't know either if it's correct:

Index: django/db/models/manipulators.py
===================================================================
--- django/db/models/manipulators.py    (revision 3116)
+++ django/db/models/manipulators.py    (working copy)
@@ -283,7 +283,7 @@
         # This is really not going to work for fields that have different
         # form fields, e.g. DateTime.
         # This validation needs to occur after html2python to be effective.
-        field_val = all_data.get(f.attname, None)
+        field_val = all_data.get(f.name, None)
         if field_val is None:
             # This will be caught by another validator, assuming the field
             # doesn't have blank=True.

comment:3 Changed 8 years ago by libraM@…

Thank you, Ivica Munitic and tom@…, for solution.

Works for me too :-)

comment:4 Changed 8 years ago by libraM@…

  • Summary changed from manipulator_validator_unique_together does not work correctly to [patch] manipulator_validator_unique_together does not work correctly

comment:5 Changed 8 years ago by libraM@…

  • Summary changed from [patch] manipulator_validator_unique_together does not work correctly to manipulator_validator_unique_together does not work correctly

Though this patch enables validation of unique_together with ForeignKeys in admin...

Sadly it also disables unique_together with blank [ForeignKey] fields (ProgrammingError: invalid input syntax for integer: "" with postgres2 when adding new record with blank ForeignKey field).

comment:6 Changed 8 years ago by ramiro

See also ticket #1751.

comment:7 Changed 8 years ago by anonymous

It works great, Thanks!.

Walter

comment:8 Changed 7 years ago by Gary Wilson <gary.wilson@…>

  • Resolution set to duplicate
  • Status changed from new to closed

marking this a duplicate of #1751. Please reopen if you think this is not the case.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.