Opened 19 years ago
Closed 19 years ago
#2019 closed defect (duplicate)
manipulator_validator_unique_together does not work correctly
| Reported by: | 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 , 19 years ago
comment:2 by , 19 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:4 by , 19 years ago
| Summary: | manipulator_validator_unique_together does not work correctly → [patch] manipulator_validator_unique_together does not work correctly |
|---|
comment:5 by , 19 years ago
| Summary: | [patch] manipulator_validator_unique_together does not work correctly → 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:8 by , 19 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
marking this a duplicate of #1751. Please reopen if you think this is not the case.
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:
When using MySQL as the database, I get: