G
G
Gerasim9712017-10-26 16:49:37
Django
Gerasim971, 2017-10-26 16:49:37

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)

Admin
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

1 answer(s)
V
Vladimir Kuts, 2017-10-26
@fox_12

Try fixing this line:
viewkitch_set - all small characters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question