1
votes

I have a Django Model with a Foreign key:

class Library:
  name=models.CharField()

class Book:
  title=models.CharField()
  library=models.ForeignKey(Library)

models.py

class BookAdmin(admin.ModelAdmin):
  extra = 0
  fields = ['title', 'library__name'] # library__name not found

admin.site.register(Book, BookAdmin)

admin.py

In the admin, I want to display Book and show an editable field for Library.name in the Book view (not the other way around with inlines):

> Book
  * Title: "Foo"
  * Library Name: "Bar"

As readonly, it's easy (just creating a method in Book model returning the library name value) but I cannot make it work for editable fields, I tried using fields=('title','library.name') and fields=('title','library__name') without success

2
Try to do like in this post answer - Altynbek

2 Answers

-2
votes

try to use related_name in models like that

library=models.ForeignKey(Library, related_name="library")

then use fields=('title','library__name') and it should work.