django Admin中有很多人

时间:2015-06-12 16:34:33

标签: django django-admin many-to-many

我想问。我可以在django admin中创建多个视图我的项目。 我有很多关系,所以我创建了一个关联实体。但我无法理解如何在django admin中查看此内容。有人可以帮助我。

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)

如果用户创建订单,我该如何查看他购买的商品?

如果管理员中的用户选择了我要查看多个项目的订单。我可以用django admin创建吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

我做过这份工作。所以这是我的代码:

admin.py

{{1}}

我采取的措施:

enter image description here

感谢所有人)))

答案 1 :(得分:0)

您应该在当前的Order_item课程中添加items = models.ManyToManyField(Item)字段,而不是使用Order课程。