How to use ManyToManyField with Django's Admin

When I specified the ManyToManyField column in list_display of admin.py, I got the following error.

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class 'proj.admin.ArticleAdmin'>: (admin.E109) The value of 'list_display[2]' must not be a ManyToManyField.

I thought it would be convenient to be able to list the data of another table linked by ManyToManyField for a certain record on the management site, but it seems that the specification cannot be specified according to the error message.

However, it was possible by doing the following, so I will record the method.

What you want to do

Imagine an example where metadata is added to a document, such as Hatena Bookmark.

イメージ図

How to respond

Suppose you have three authors, an Article, and a Tag.

Article.meta is ManyToManyField, and Tag is specified as the relation destination.

models.py

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

class Article(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey('Author', blank=True, null=True)
    meta = models.ManyToManyField('Tag')

    def __unicode__(self):
        return self.title

class Tag(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

admin.py

If you define a function with an arbitrary name such as _meta and specify the corresponding function name in list_display, you can list the data linked by commas as shown in the figure above.

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', '_meta')

    def	_meta(self, row):
        return ','.join([x.name for x in row.meta.all()])

admin.site.register(Article, ArticleAdmin)

Reference material

Recommended Posts

How to use ManyToManyField with Django's Admin
How to use Django's GeoIp2
Python: How to use async with
How to use virtualenv with PowerShell
How to use FTP with Python
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to use Ass / Alembic with HtoA
How to use Japanese with NLTK plot
How to use jupyter notebook with ABCI
How to use CUT command (with sample)
How to use SQLAlchemy / Connect with aiomysql
How to view images in Django's Admin
How to use JDBC driver with Redash
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use GCP trace with open Telemetry
How to use tkinter with python in pyenv
How to use Qt Designer
How to use xgboost: Multi-class classification with iris data
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to get a logged-in user with Django's forms.py
How to deal with Django's Template Does Not Exist
How to use FastAPI ③ OpenAPI
How to use python interactive mode with git bash
How to use Python argparse