简体   繁体   English

Django中2个型号的关系

[英]Relationship between 2 models in Django

I have two models- Order and Expense.我有两个模型——订单和费用。 The expense is a crud list respective to the particular Order.费用是与特定订单相关的粗略清单。 How can I make such a relationship between these two models?我怎样才能在这两个模型之间建立这样的关系?

models.py - Order models.py - 订单

class Order(models.Model):

    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="order",blank=True,null=True)
    client_name = models.CharField(max_length=100)
    event_name = models.CharField(max_length=100)
    contact = models.CharField(max_length=15)
    event_date = models.DateField(auto_now_add=False,auto_now=False)
    expenses = models.IntegerField(default=0,null=True,blank=True)

Models.py - Expense Models.py - 费用

class ProjectExpense(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="project_expense",null=True,blank=True)
    order_id = models.IntegerField(default='0')
    exp = models.CharField(max_length=100)
    exp_desc = models.TextField(null=True,blank=True)
    amount = models.IntegerField(default='0')

    def __str__(self):
        return self.exp

I tried assigning the field Order ID with the current Order.我尝试使用当前订单分配字段订单 ID。 But how will I able to pass the current Order.但是我将如何通过当前的订单。

I'm guessing you need a ManyToOne Relationship: One Order can have multiple expenses?我猜你需要多对一关系:一个订单可以有多项费用?

For that you can remove the expenses property from the Order model.为此,您可以从订单 model 中删除费用属性。 And you need to modify the order_id property of the ProjectExpense model like this (as @Lambo already commented):您需要像这样修改 ProjectExpense model 的 order_id 属性(正如@Lambo 已经评论过的那样):

order = models.ForeignKey(Order, on_delete=models.CASCADE)

When you are querying the Order model the related Expenses will be included if you ask for them and vice versa.当您查询订单 model 时,如果您要求相关费用将包括在内,反之亦然。

The Objects are created as following in the backend:对象在后端创建如下:

new Order:新命令:

# input contains the data received from frontend
Order.objects.create(user_id=input.user_id,
                     client_name=input.client_name,
                     event_name=input.event_name,
                     contact=input.contact,
                     event_date=input.event_date)

new Expense:新费用:

# input contains expense details received from frontend
ProjectExpense.objects.create(user_id=input.user_id,
                              order_id=input.order_id,
                              exp=input.exp,
                              exp_desc=input.exp_desc,
                              amount=input.amount)

It is important that you write order_id= although the property in the model itself is called order .尽管 model 本身的属性称为order ,但编写order_id=很重要。

Thanks all for providing me the answers.感谢大家为我提供答案。 The feature I'm looking at is GenericForeignKey.我正在查看的功能是 GenericForeignKey。 This tutorial helped me understand it.教程帮助我理解了它。

The working of Expenses associated with the respective Order, is way similar to the Comments and Post like in blogs.与相应订单相关的费用的工作方式类似于博客中的评论和帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM