Opened 18 years ago

Closed 18 years ago

#2019 closed defect (duplicate)

manipulator_validator_unique_together does not work correctly

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

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 :))

Change History (8)

comment:1 by tom@…, 18 years ago

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 by tom@…, 18 years ago

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 by libraM@…, 18 years ago

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

Works for me too :-)

comment:4 by libraM@…, 18 years ago

Summary: manipulator_validator_unique_together does not work correctly[patch] manipulator_validator_unique_together does not work correctly

comment:5 by libraM@…, 18 years ago

Summary: [patch] manipulator_validator_unique_together does not work correctlymanipulator_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 by Ramiro Morales, 18 years ago

See also ticket #1751.

comment:7 by anonymous, 18 years ago

It works great, Thanks!.

Walter

comment:8 by Gary Wilson <gary.wilson@…>, 18 years ago

Resolution: duplicate
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top