【发布时间】:2022-01-20 12:22:57
【问题描述】:
我目前正在做一个小项目。有两个模型很有趣:
-仓库
-仓库转移之间
注意:每个仓库都有多个不同型号的物品(即产品);
问题:我希望能够将物品从一个仓库转移到另一个仓库。为此,在 BetweenWarehouseTransfers 中,我希望有一个字段指定我将说话物品转移到的仓库(to_warehouse),以及一个指定该物品来自哪个仓库的字段(from_warehouse)。稍后我想将 autocomplete_fields 添加到这两个仅接受现有仓库作为输入的字段。
有没有人知道如何创建这种循环关系。
我已经发布了几个我设想的解决方案,但每次都会出现循环关系问题:
#0。 (原版)
from django.db import models
from django_extensions.db.models import TimeStampedModel
# from supply_chain.models.warehouse import Warehouse
class WarehouseTransferProductVariant(TimeStampedModel):
product_variant = models.IntegerField()
amount = models.IntegerField()
to_warehouse_id = models.CharField(blank=False, max_length=255)
from_warehouse_id = models.CharField(blank=False, max_length=255)
StatusChoices = models.TextChoices(
"StatusChoices", "SENTREQUEST ACCEPTED DECLINED CANCELLED"
)
status = models.CharField(blank=True, choices=StatusChoices.choices, max_length=255)
# relation_from_to = models.ManyToManyField('warehouse.Warehouse', related_name="fromwarehouse_towarehouse")
class FromWarehouseTransferProductVariant(TimeStampedModel):
warehouse = models.ForeignKey(Warehouse, on_delete=models.RESTRICT)
from_warehouse = models.CharField(max_length=255)
class ToWarehouseTransferProductVariant(TimeStampedModel):
warehouse = models.ForeignKey(Warehouse, on_delete=models.RESTRICT)
to_warehouse = models.CharField(max_length=255)
productvarariant = models.IntegerField()
amount = models.IntegerField()
StatusChoices = models.TextChoices(
"StatusChoices", "SENTREQUEST ACCEPTED DECLINED CANCELLED"
)
status = models.CharField(blank=True, choices=StatusChoices.choices, max_length=255)
class WarehouseTransferProductVariant(TimeStampedModel):
productvarariant = models.IntegerField()
amount = models.IntegerField()
to_warehouse = models.CharField(blank=False, max_length= 255)
from_warehouse = models.CharField(blank=False, max_length= 255)
StatusChoices = models.TextChoices(
"StatusChoices", "SENTREQUEST ACCEPTED DECLINED CANCELLED"
)
status = models.CharField(blank=True, choices=StatusChoices.choices, max_length=255)
to_warehouse_id = models.ForeignKey(Warehouse)
from_warehouse_id = models.ForeignKey(Warehouse)
type_of_relation = models.SmallPositiveIntegerField(choices=[(1,'from'), (2, 'to')])
提前致谢。
【问题讨论】:
标签: python django database model