Inline in Django admin: has no ForeignKey
django, python
Solution
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.
Problem
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?