A customer model to a order model that has price values in Django

只愿长相守 提交于 2019-12-12 05:29:54

问题


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.

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.

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

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.


回答1:


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.

Here's an example:

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.




回答2:


You should probably read about django Widgets and then use SelectMultiple widget.

Btw. Please, paste your code. It would be much easier to help.



来源:https://stackoverflow.com/questions/43810254/a-customer-model-to-a-order-model-that-has-price-values-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!