Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7911 closed (duplicate)

Model Inheritance still doesn't work in the admin.

Reported by: atomax Owned by: brosner
Component: contrib.admin Version: master
Severity: Keywords: admin interface, model inheritance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Ticket #6755 was supposed to update newforms-admin to support model inheritance (as per queryset-refactor). All of this has been merged to trunk and ticket #6755 has been closed.

Still, model inheritance does not work in the admin. There is an error thrown up as follows (seen in 1.0-alpha-SVN-8061):

Exception at /admin/news/review/33/
<class 'coolsite.news.models.Comment'> has no ForeignKey to <class 'coolsite.news.models.Review'>
Request Method: 	GET
Request URL: 	        http://localhost:80000/admin/news/review/33/
Exception Type: 	Exception
Exception Value: 	<class 'coolsite.news.models.Comment'> has no ForeignKey to <class 'coolsite.news.models.Review'>
Exception Location: 	c:\usr\django\trunk\django\forms\models.py in _get_foreign_key, line 454

In fact, Comment has a foreign key to Item and Review is a subclass of Item, so the foreign key should be inherited. This is the case, because it can be accessed in the shell. But the admin interface cannot find it.

Here is the relevant code:

model.py
========
class Item(models.Model):
    title = models.TextField(null=True, blank=True)

class Comment(models.Model):
    item = models.ForeignKey(Item, related_name='comments', null=True, blank=True)
    text = models.TextField(null=True, blank=True)

class Review(Item):
    text = models.TextField(null=True, blank=True)

class Announcement(Item):
    text = models.TextField(null=True, blank=True)


admin.py
========

class CommentInline(admin.StackedInline):
    model = Comment
    extra = 1

class ItemOptions(admin.ModelAdmin):
    model = Item
    inlines = [CommentInline]

class ReviewOptions(admin.ModelAdmin):
    model = Review
    inlines = [CommentInline]

class AnnouncementOptions(admin.ModelAdmin):
    model = Announcement
    inlines = [CommentInline]

admin.site.register(Comment)
admin.site.register(Item, ItemOptions)
admin.site.register(Review, ReviewOptions)
admin.site.register(Announcement, AnnouncementOptions)

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by brosner

  • Owner changed from nobody to brosner
  • Status changed from new to assigned

People have been filing tickets with too generic ticket titles (hey, including this one too) which makes you believe that #6755 solved all model inheritance issues in the admin. That is not the case. The revision that closed the ticket as fixed fixed the problem that was described. There can always be outstanding issues :)

I will take a look into this today.

comment:2 Changed 6 years ago by brosner

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

Closing in favor of #7918. It has a patch and is a bit more specific along with details.

comment:3 Changed 6 years ago by atomax

great, thanks a lot Brian!

and sorry for the unspecific title.

comment:4 Changed 3 years ago by jacob

  • milestone 1.0 beta deleted

Milestone 1.0 beta 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.