problem implementing admin inline

78 views
Skip to first unread message

yakka...@gmail.com

unread,
Nov 21, 2014, 12:22:04 AM11/21/14
to django...@googlegroups.com
I have a <article> model that has a foreignKey to a <module> model.  I want to be able to edit the <module> model within the <article> admin

in moduleApp.models I have something like:
class module(models.Model):
  HTML = models.TextField( blank=True, null=True )

in articleApp.models I have something like:
class article(models.Model)
  HomePageModule = models.ForeignKey(module, blank=True, null=True)

In the admin I have something:
class moduleInline(admin.TabularInline):
    model = module

class ArticleAdmin(admin.ModelAdmin):
    inlines = [moduleInline,]
   
I'm getting the error XXX has no ForeignKey to XXX.

I've found a few other people with the problem and it seems like the problem is that I need to change up the moduleInline to articleInline.
http://stackoverflow.com/questions/21899523/inline-in-django-admin-has-no-foreignkey
http://stackoverflow.com/questions/8526159/django-inline-has-no-foreignkey-to

I'm trying to keep my moduleApp as a stand alone app and use it in multiple other apps.  To achieve this I'll have to move all the admin code in the module app.  This defeats my desire to make moduleApp a stand alone app. 

Is there a way I can make inlines work for this?  If not, what is the best way to get this done?  I've got a highly customized admin so I don't mind making the edits. 

Brian

Collin Anderson

unread,
Nov 21, 2014, 6:05:56 PM11/21/14
to django...@googlegroups.com
Hi,

Here's a hack that might solve your problem.

# moduleApp/admin.py
class ModuleAdmin(admin.ModelAdmin):
    inlines
= []
   
# etc

admin
.site.register(Module, ModuleAdmin)


# articleApp/admin.py
from moduleApp.admin import ModuleAdmin

class ArticleInline(admin.StackedInline):
    model
= Article
   
# etc

ModuleAdmin.inlines.append(ArticleInline)

Collin
Reply all
Reply to author
Forward
0 new messages