10

I have the following models:

class A(models.Model):
    name = models.CharField(max_length=100)
    c = models.ForeignKey(C, related_name='letters_c', null=True, blank=True)
    ...


class B(models.Model):
    id= models.CharField(max_length=200, blank=False, default="")
    a = models.ForeignKey(A, related_name='letters_a', default=0)
    ...

With the following admin:

class AAdmin(admin.ModelAdmin):
    fields = ['name', 'c', 'letters_a']
    list_display = ['name']
    ordering = ['name']

I got an error

'letters_a' not found.

I guess I do not fully understand the logic between foreign keys and the representation of them in Django models.

I want to be able to add/edit and see in the Django admin the class A letters_a related_name objects.

How can I do it?

1 Answers1

11

To have related model in your admin use InlineAdmin

in your case, add inline admin definition for class B:

class BInlineAdmin(admin.TabularInline):
    model = B

class AAdmin(admin.ModelAdmin):
     fields = ['name', 'c']
     list_display = ['name']
     ordering = ['name']
     inlines = [BInlineAdmin]
Jerzyk
  • 3,662
  • 23
  • 40