Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#7911 closed (duplicate)

Model Inheritance still doesn't work in the admin.

Reported by: atomax Owned by: Brian Rosner
Component: contrib.admin Version: dev
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: no UI/UX: no

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)

Change History (4)

comment:1 by Brian Rosner, 16 years ago

Owner: changed from nobody to Brian Rosner
Status: newassigned

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 by Brian Rosner, 16 years ago

Resolution: duplicate
Status: assignedclosed

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

comment:3 by atomax, 16 years ago

great, thanks a lot Brian!

and sorry for the unspecific title.

comment:4 by Jacob, 13 years ago

milestone: 1.0 beta

Milestone 1.0 beta deleted

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