Answer the question
In order to leave comments, you need to log in
How to merge and display two django models in one admin model?
Hello everyone, I need help with the django admin.
There is a product, the product can have 100+ colors.
Product one table, colors another table
How, when creating a product through the django admin, add the desired colors from the color table for the product without switching to another color model?
Visualization of the question:
models.py
#coding:utf-8
from django.db import models
class Products(models.Model):
product_name = models.CharField(max_length=50,default='none',verbose_name='Название')
about = models.CharField(max_length=50,default='none',verbose_name='Информация')
class Meta:
verbose_name = "Продукт"
verbose_name_plural='Продукты'
def __unicode__(self):
return self.product_name
class Colors(models.Model):
s = models.ForeignKey(Products)
white = models.BooleanField(default=False)
yellow= models.BooleanField(default=False)
green= models.BooleanField(default=False)
class Meta:
verbose_name_plural='Цвета'
### 100+ colors
Answer the question
In order to leave comments, you need to log in
You need to create a refillable color guide. And paste it inline in the admin panel. (Only it seems to me that getting a double nesting, in the dzhang-admin panel, it seems like only 1 inline level can be.)
class ProductColors(models.Model):
s = models.ForeignKey(Products)
colors = models.ManyToManyField(to=Colors)
class Colors(models.Model):
name = models.CharField(verbose_name="Название цвета")
Hey!
If I understand the question correctly, then you can apply admin.StackedInline
admin.py
from django.contrib import admin
from models import Colors, Products
class ColorsInLine(admin.StackedInline):
model = Colors
@admin.register(Products)
class ProductsAdmin(admin.ModelAdmin):
inlines = [ColorsInLine,]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question