Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying both sides of a ManyToMany relationship in Django admin

Say I have the following models that have a many-to-many relationship:

models.py:

class Foo(models.Model):

    name = models.TextField()


class Bar(models.Model):

    name = models.TextField()
    foos = models.ManyToManyField(Foo, related_name='bars')

And then having defined them in admin in the following way:

admin.py

@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
    """Foo admin."""

    list_display = ('name',)
    search_fields = ('name',)


@admin.register(Bar)
class BarAdmin(admin.ModelAdmin):
    """Bar admin."""

    list_display = ('name',)
    search_fields = ('name',)

In Django admin, when browsing Bar instances, I can see the Foo instances Bar is associated with and can modify them from there.

However, no such luck with Foo, I can't see the Bar instances that every Foo object is associated with.

Can Django define automatic handling for this or would I need to roll my own methond?

I'm using Python 3.6.1 and Django 1.11.

like image 526
Nobilis Avatar asked May 10 '17 13:05

Nobilis


People also ask

Can I use Django admin as frontend?

Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.

How does many-to-many field work in Django?

A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.

Is there any documentation on many-to-many relationships in Django?

NOTE - Documentation on more operations can be found in official Django documentation on many-to-many relationships. Many To Many relationships and representative associative table consists of ForeignKeys, Django only made it easier to implement it with ManyToManyField.

What is manytomanyfield in Django?

Django ManyToManyField is a field that helps you create many to many relationships in Django. It’s also the most obvious and fastest way. **options – optional arguments explained in official Django documentation ManyToManyField between two models creates a third table in the database. That table contains the primary key of both models.

How to choose related objects to a model in Django?

Django admin interface adds a widget for choosing related objects to a model where ManyToManyField is set. In other words, it requires adding referenced objects first. For our example, you could choose authors only when adding books, but not another way around. After migrating the above models to the database, we get the following database schema:

Does it matter where a table is placed in Django?

From the database point of view, it doesn’t matter. Tables are equally connected no matter where you place it. But Django admin and ORM have slight changes in behavior. Django admin interface adds a widget for choosing related objects to a model where ManyToManyField is set. In other words, it requires adding referenced objects first.


1 Answers

You could define a custom InlineModelAdmin like so:

class BarInline(admin.TabularInline):
    model = Bar.foos.through

and use it in your FooAdmin:

class FooAdmin(admin.ModelAdmin):
"""Foo admin."""
    model = Foo
    inlines = [
        BarInline,
    ]

Have a look at this part of the django documentation.

like image 107
Hopiu Avatar answered Oct 08 '22 00:10

Hopiu