Read fields from ManyToMany field via InlineModel

I can view users’ books and the user of the books in the admin panel. I want to view the name and isbn number of the book in tabular inline but I cannot access them

User and Uook are connected by a many to many relationship

class UserInline(admin.TabularInline):
    model = Book.author.through

class BookInline(admin.TabularInline):
    model = User.book.through

@admin.register(User)
class UserAdmin(UserAdmin):
    list_filter = []
    list_display = ['username', 'email']
    list_per_page = 20
    search_fields = ('username', 'email', 'first_name', 'last_name')
    inlines = [BookInline,]

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ['isbn', 'title', 'isbn']
    list_per_page = 20
    search_fields = ('isbn', 'title', 'author__username')
    inlines = [UserInline,]
class UserBook(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)

    def __str__(self):
        return ''
    
    class Meta:
        db_table = 'website_user_book'

The error I get when I write fields = (‘title’, ‘isbn’) in the user inline class:

Exception Type: FieldError at /admin/website/book/3488/change/
Exception Value: Unknown field(s) (email, username) specified for UserBook

See the docs at Working with many-to-many models.

  • From the docs:

… The through attribute is a reference to the model that manages the many-to-many relation. …

This means you only define one inline, and it’s for the model containing the ManyToManyField.

If you haven’t defined the ManyToManyField in one of the models, you should. Also, if you don’t need to store additional data on the UserBook model, you can probably remove it.

See the rest of the details regarding the use of the inlines with an M2M for the use of the inlines.

My purpose of using the UserBook class is to change the object name written above the table list. Such as, “User_Book_object”

def __str__(self):
    return ''

If there is a solution where I can change this field, I will not need to use the UserBook class

I’m sorry, I don’t understand what you’re referring to here.

1

This

You can keep the model definition and use it as the “through” class in your ManyToManyField.

If you’re looking to remove that label from being rendered, you can also copy the existing template (django.contrib.admin.templates.admin.edit_inline.tabular.html) to the templates directory for your app, remove the line that causes the label to be rendered (It’s the line: {{ inline_admin_form.original }} ), and specify that template to be the template to use for that inline. (See InlineModelAdmin.template)