T
T
Tash1moto2015-10-07 12:33:30
Django
Tash1moto, 2015-10-07 12:33:30

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:
ec8b3-clip-82kb.jpg?nocache=1
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

3 answer(s)
R
Rostislav Grigoriev, 2015-10-07
@crazyzubr

InlineModelAdmin objects with extra parameter

S
Salavat Sharapov, 2015-10-07
@desperadik

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="Название цвета")

Kind of like that.
In admin.py create a TabularInline and paste its Products admin class.

P
Petr Kashyapov, 2015-10-07
@kashyapov

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 question

Ask a Question

731 491 924 answers to any question