Answer the question
In order to leave comments, you need to log in
How to display many-to-many fields in django?
I wrote a method in the admin panel that should display a column, but it does not appear
Models
from django.db import models
class Kitch(models.Model):
name = models.CharField(max_length=200)
def __str__ (self):
return "{}".format(self.name)
class ViewKitch(models.Model):
Kitch = models.ManyToManyField(Kitch)
namecook = models.CharField(max_length=200)
def __str__ (self):
return "{} {}".format(self.namecook)
from django.contrib import admin
from .models import *
class KitchAdmin(admin.ModelAdmin):
def kitchname(self, obj):
return ", ".join([str(i.namecook) for i in obj.ViewKitch_set.all()])
list_display = ['name', 'kitchname']
admin.site.register(Kitch, KitchAdmin)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question