Many to many in django Admin

1k views Asked by At

I want to ask. Can i create multiple view my items in django admin. I have a many to many relationship, so i created an associative entity. But i can't understand how view this in django admin. Can someone help me.

models.py

from django.db import models
from django.contrib import admin

class Client(models.Model):
    f_name = models.CharField(verbose_name = "Фамилия", max_length = 100)
    l_name = models.CharField(verbose_name = "Имя", max_length = 100)
    m_name = models.CharField(verbose_name = "Отчество", max_length = 100)
    phone = models.CharField(verbose_name = "Телефон", max_length = 100)
    city = models.CharField(verbose_name = "Город", max_length = 100)
    address = models.CharField(verbose_name = "Адрес", max_length = 200)

    def __str__(self):
        return self.f_name + " " + self.l_name + " (" + self.phone + ")"

class Item(models.Model):
    name = models.CharField(verbose_name = "Название", max_length = 100)
    TYPE_ITEMS = (
        ("shirt", "Футболка"),
        ("shoes", "Обувь"),
        ("bags", "Рюкзаки и сумки"),
        ("heads", "Головные уборы"),
        ("others", "Другое"),
        )
    type_item = models.CharField(verbose_name = "Тип продукта",
                                 choices = TYPE_ITEMS, max_length = 6,
                                 default = "shirt")
    other = models.TextField("другая информация")
    color = models.CharField("Цвет(а)", max_length = 100)
    cost = models.IntegerField("Стоимость за штуку", default = 0)
    is_available_now = models.BooleanField("Есть ли в наличии?",
                                           default = False)
    available_count = models.IntegerField("Количество в наличии", default = 0)
    photo = models.ImageField("Фото", upload_to = "media")

    def __str__(self):
        return self.name + " " + self.color + " (" + str(self.cost) + " грн)"

class Order(models.Model):
    id_client = models.ForeignKey(Client, null = True, blank = True,
                                  verbose_name = "Клиент")
    date_order = models.DateField(verbose_name = "Дата заказа")
    date_taken = models.DateField(verbose_name = "Дата получения")
    is_paid = models.BooleanField(verbose_name = "Оплачено?",  default = False)
    is_taken = models.BooleanField(verbose_name = "Получил покупатель?",
                                   default = False)

class Order_item(models.Model):
    id_order = models.ForeignKey(Order)
    id_item = models.ForeignKey(Item)

admin.py

admin.site.register(Item)
admin.site.register(Order)
admin.site.register(Client)
admin.site.register(Order_item)

If user creates order, how can i view what he buy?

If user in admin selects an order i want to view many items. Can i create this with django admin ?

enter image description here

2

There are 2 answers

2
Joan Nadal Brotat On

Instead of use Order_item class, you should add a items = models.ManyToManyField(Item) field into your current Order class.

0
Oleg Klimenko On

I have done this job. So this is my code:

admin.py

from django.contrib import admin

from myapp.models import Client, Item, Order, Ordering

class ClientAdmin(admin.ModelAdmin):
    list_display = ("f_name", "l_name", "m_name", "phone", "city", "address")
    list_filter = ["f_name", "l_name", "city"]

class ItemAdmin(admin.ModelAdmin):
    list_display = ("name", "type_item", "color", "cost", "is_available_now",
                    "available_count")
    list_filter = ["name", "type_item", "color", "cost", "available_count"]

class OrderingAdmin(admin.ModelAdmin):
    list_display = ("id_order", "id_item", "count_items")
    list_filter = ["id_order", "id_item", "count_items"]

class OrderingInline(admin.TabularInline):
    model = Ordering
    fk_name = "id_order"
    max_num = 1

class OrderAdmin(admin.ModelAdmin):
    list_display = ("id", "id_client", "date_order", "date_taken", "is_paid",
                    "is_taken")
    list_filter = ["date_order", "date_taken", "is_paid", "is_taken"]

    inlines = [
        OrderingInline,
        ]

admin.site.register(Item, ItemAdmin)
admin.site.register(Order, OrderAdmin)
admin.site.register(Client, ClientAdmin)
admin.site.register(Ordering, OrderingAdmin)

And what I take:

enter image description here

Thanks to all)))