#29649 closed Bug (duplicate)
admin readonly_fields with unique_together raises IntegrityError
Reported by: | Harm Verhagen | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 2.0 |
Severity: | Normal | Keywords: | unique_together IntegrityError |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I'm getting an IntegrityError when editing a model in the admin, when one of the fields of a unique_together pair is in readonly_fields
example
#models.py class Foo(models.Model): a = models.IntegerField() b = models.IntegerField() class Meta: unique_together = (('a', 'b'), ) #admin.py class FooAdmin(admin.ModelAdmin): list_display = ('id', 'a', 'b') readonly_fields = ('b', )
steps to reproduce
In django shell create two instances
>>> from data.models import Foo >>> Foo.objects.create(a=1,b=1) <Foo: Foo object (1)> >>> Foo.objects.create(a=2,b=1) <Foo: Foo object (2)>
Then in the admin edit Foo object 2 and change a 2 -> 1. (introducing a duplicate)
expected result
A form validation error in the admin: Foo with this A and B already exists.
actual result
A server 500 error (IntegrityError)
IntegrityError at /admin/data/foo/2/change/ duplicate key value violates unique constraint "data_foo_a_b_ac92595b_uniq" DETAIL: Key (a, b)=(1, 1) already exists.
notes
When removing the readonly_fields
from the admin, all works as expected.
Maybe related to #13091 (similar issue when list_editable is used instead of readonly_fields)
Change History (4)
comment:1 by , 6 years ago
Description: | modified (diff) |
---|
comment:2 by , 6 years ago
Description: | modified (diff) |
---|
comment:3 by , 6 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:4 by , 6 years ago
Component: | Uncategorized → Forms |
---|
I think this is a duplicate of #13091.
Yes it's slightly different in that it's using
read_only_fields
but the bottom line is that the error is caused by the form not being able to validate aunique_together
constraint where one of the fields is excluded. (That's the underlying issue from #13091.)