在Django中具有价格值的订单模型的客户模型 [英] A customer model to a order model that has price values in Django

查看:201
本文介绍了在Django中具有价格值的订单模型的客户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Django构建餐厅管理应用程序,我的客户模型具有名称,订单,表整数字段和is_active布尔值字段.我有一个工作表,可以创建一个Customer obj并为其分配以逗号分隔的订单,然后使用模型方法进行呈现.

I was trying to build a restaurant management app using Django, my Customer model has a name, orders, a table integer field and an is_active boolean field. I have a working form up that creates a Customer obj and assigns it its orders separated by commas, which I then render using a model method.

我想做的是在客户"字段中有多个订单字段,然后将某个订单字段直接链接到固定价格.

What I want to do is have multiple order fields in a Customer field and then a certain order field should be linked directly to a fixed price.

我对Django还是很陌生,所以这是我的最佳解释:创建客户对象时,您应该看到一个名称字段,多个订单字段,其中还应该包含一些数量字段,然后都应该将它们链接在一起到固定价格,因为我将用它来计算总价:fixed_price_of_order乘以quantity_ordered.

I'm quite new to Django, so here's how I can explain it best: when you create a customer object, you should see a Name field, multiple order fields which should also have some quantity fields which should both then be linked to a fixed price because I will use that to calculate a total price: fixed_price_of_order multiplied by the quantity_ordered.

我试图找到类似的东西,但是我还没有真正能够找到. Django可能不是最好的工具,但这是我必须使用的.

I have tried to find something similar, but I haven't really been able to yet. Django might not be the best tool for this, but this is what I have to use.

编辑 这就是我今天要拥有的东西,比昨天有了更多的东西,也就是我可以手动输入订单,但这容易出错,使用起来也不是很有趣.

EDIT Here's what I have today, got a bit further than yesterday, aka I can type in the orders manually, but that's error prone and not exactly fun to use.

class Item(models.Model):
    name = models.CharField(max_length=120)
    price = models.IntegerField(default=0, unique=False)

    def __str__(self):
        return self.name

class Customer(models.Model):
    name = models.CharField(blank=False, max_length=120, unique=True)
    order = models.ManyToManyField(Item)
    table = models.IntegerField(blank=True)
    pub_date = models.DateTimeField()
    is_active = models.BooleanField(default=True)
    def __str__(self):
        return self.name

现在我需要的是一种跟踪数量的方法,我考虑过向Item中添加一个数量字段,但这将不起作用,因为并非每个订单都具有相同数量的Item.我查了一下,看到一些有关贯通表的东西,无法理解它是什么或使用它的方式.我现在可以接受手动输入的字段.

Now what I need is a way to track quantities, I thought about adding a quantity field to Item, but that wouldn't work because not every order has the same quantity of Item. I looked it up and saw some stuff about a through table, couldn't understand what that is or any way to use it. I can live with the manual field entering for now.

推荐答案

据我了解,您需要3个表, 客户,有关特定客户的详细信息, 项,对于特定项, 订单,每个客户的每笔订单.

From what I understand, you need 3 tables, Customer, for the details of a specific customer, Item, for a specific item, Order, for every order each customer makes.

这是一个例子:

class Item(models.Model):
    name = ...........
    price = ..........

class Customer(models.Model):
    name = .........
    ..........

class Order(models.Model):
    customer = models.ForeignKey(Customer, related_name='orders', null=True, blank=True)
    item = models.ForeignKey(Item, related_name='orders', null=True, blank=True)
    order_date = models.DateField(auto_now=True)
    is_cancelled = models.BooleanField(default=False)
    is_delivered = models.BooleanField(default=False)
    quantity = models.IntegerField()

这样,您可以让一个客户拥有与单个或多个项目相关的多个订单.数量字段可以添加到订单"表中,以便每个订单可以拥有自己的数量.

By doing like this, you can have a single customer with multiple orders related to single or multiple items. The quantity field can be added to the Order table, so that each order can have its own quantity.

这就是我想的那样,希望您得到了想要的东西.

This is just what I thought how it can be, Hope you got what you were looking for.

这篇关于在Django中具有价格值的订单模型的客户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆