Django-连接3个表以获取记录

时间:2019-04-26 06:39:08

标签: python django

需要加入这3个表,有人可以给我ORM查询集吗?需要基于这三个表在一个模板上显示记录。

Models.py

class Item_Table(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=127)
    description = models.TextField(null=True,blank=True)
    qty_in_ferma_warehouse = models.IntegerField(null=True,blank=True,default=0)
    status = models.BooleanField()
    ListID = models.CharField(max_length=80, null=True,blank=True)


class Order_Table(models.Model):
    id = models.AutoField(primary_key=True)
    order_number = models.CharField(max_length=63)
    notes = models.TextField()
    created_at = models.DateField()
    status = EnumField(Choice,max_length=2, null=True,blank=True)
    Total_Amount = models.DecimalField(max_digits=18,decimal_places=2)
    must_fill_in_items = models.IntegerField()
    total_replenish_small_order = models.IntegerField()
    order_type = EnumField(Choice3,max_length=2, null=True,blank=True)
    delivery_method = EnumField(Choice6, max_length=2, null=True, blank=True)



class Orderitems_Table(models.Model):
    id = models.AutoField(primary_key=True)
    order_id = models.ForeignKey(Order_Table,on_delete='')
    item_id = models.ForeignKey(Item_Table,on_delete='')
    qty = models.IntegerField()
    next_order_qty = models.IntegerField(null=True,blank=True,default=0)
    Rate = models.DecimalField(max_digits=18,decimal_places=2)
    Amount = models.DecimalField(max_digits=18,decimal_places=2)
    status = EnumField(Choice2,max_length=2,null=True,blank=True)
    accumulated_residual_item = models.IntegerField()
    accumulated_damaged_item = models.IntegerField()

1 个答案:

答案 0 :(得分:0)

您可以在Order_Table中使用多对多字段。

items = models.ManyToManyField(Person, through='Orderitems_Table')

然后您可以在Order_Table模型上进行查询并拥有一个查询集

您可以浏览documentation,以深入了解ManyToMany字段以及如何使用它

对于表格和视图,您可以关注this link