Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#10769 closed (duplicate)

IntegrityError with generic relations, unique_together, and inline admin forms

Reported by: rutt Owned by: nobody
Component: contrib.admin Version: 1.1-beta
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Using the models and admin below, create a new GenericAttachment object using the inline form on the Parent change page. Then create another one with the same slug. The result is an IntegrityError where "columns content_type_id, object_id, slug are not unique". This works fine for Attachment objects.

Models:

class Parent(models.Model):
    name = models.CharField(max_length=200)
    
class Attachment(models.Model):
    name = models.CharField(max_length=200)
    parent = models.ForeignKey(Parent)
    slug = models.SlugField()
    
    class Meta:
        unique_together = ['parent', 'slug']
    
class GenericAttachment(models.Model):
    name = models.CharField(max_length=200)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    parent = generic.GenericForeignKey()
    slug = models.SlugField()
    
    class Meta:
        unique_together = ['content_type', 'object_id', 'slug']

Admin:

class GenericAttachmentInline(generic.GenericTabularInline):
    model = GenericAttachment
    
class ParentAdmin(admin.ModelAdmin):
    inlines = [AttachmentInline, GenericAttachmentInline]

admin.site.register(Attachment, admin.ModelAdmin)
admin.site.register(GenericAttachment, admin.ModelAdmin)
admin.site.register(Parent, ParentAdmin)

Attachments (0)

Change History (4)

comment:1 Changed 5 years ago by rutt

  • Component changed from Uncategorized to django.contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by rutt

See also #10770

comment:3 Changed 5 years ago by mrts

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

Duplicate of #9493 .

comment:4 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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.