4

I have the following in admin.py

class AdInline(admin.StackedInline):
    model = Ad

class UnitAdmin(admin.ModelAdmin):
    fields = ('user', 'name', 'about', 'url', 'active', 'type')
    list_display = ('user', 'name', 'url', 'created', 'active', 'type')

    inlines = [AdInline]

class AdAdmin(admin.ModelAdmin):
    fields = ('user', 'title', 'about', 'url', 'active')
    list_display = ('user', 'title', 'url', 'created', 'active', 'clicks')

Now this is done in mongodb so i don't want relationships.. but I want admin to work.. When I try to load up a unit I get the following error

Exception Value:    <class 'ad.models.Ad'> has no ForeignKey to <class 'ad.models.Unit'>

Ad has no foreign key what so ever. I want all the ads to live inside a unit as a dictionary in mongodb. I just want to leverage the power of django admin's to create them.

any pointers?

Mike
  • 7,769
  • 13
  • 57
  • 81

1 Answers1

5

Your inline has to have a foreign key to the model you want to include it in, i.e. your Ad needs a foreign key to your unit; that's the way it works. If you can't represent that relationship, then you can't use inlines. I'm not sure what can replicate this behaviour in the admin, but I don't think inlines fit the bill.

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • 12
    It is a shame it's not possible. I can easily imagine cases where a model A does not in fact have foreign keys to the inline model B, but a `QuerySet` of Bs can be provided via an overridden `InlineModelAdmin`'s `get_queryset()` using A's fields/methods. – gregoltsov Feb 26 '14 at 16:15