I have two Django models:
class Author(models.Model):
user_email = models.CharField(max_length=100, blank=True)
display_name = models.CharField(max_length=250)
class Photo(models.Model):
author = models.ForeignKey(Author)
image = ThumbnailImageField(upload_to='photos')
To get inline photos, I have in admin.py:
class PhotoInline(admin.StackedInline):
model = Author
class AuthorAdmin(admin.ModelAdmin):
list_display = ('display_name','user_email')
inlines = [PhotoInline]
I get an error: Exception at /admin/metainf/author/11/
<class 'metainf.models.Author'> has no ForeignKey to <class 'metainf.models.Author'>
Why?
The inline model should be having a ForeignKey to the parent model. To get Photo
as inline in Author
your models code is fine. But your admin code should be as follows:
class PhotoInline(admin.StackedInline):
model = Photo
class AuthorAdmin(admin.ModelAdmin):
list_display = ('display_name','user_email')
inlines = [PhotoInline]
Read more info here.
That's because Author
doesn't have a foreign key to photo. I think you need to switch the model for the inline like this:
class PhotoInline(admin.StackedInline):
model = Photo
class AuthorAdmin(admin.ModelAdmin):
list_display = ('display_name','user_email')
inlines = [PhotoInline]
Maybe you need to install django-nested-admin library, and later try with NestedStackedInline. django-nested-admin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With