【问题标题】:Django - creating a model with circular relationship - from instance to another instance?Django - 创建具有循环关系的模型 - 从实例到另一个实例?
【发布时间】: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


【解决方案1】:

这就是我会做的:

class WarehouseTransferProduct(TimeStampedModel):
    product = models.ForeignKey('Product')
    to_warehouse = models.ForeignKey('Warehouse', related_name='to_warehouse')
    from_warehouse = models.ForeignKey('Warehouse', related_name='from_warehouse')
    amount = models.IntegerField()

    StatusChoices = models.TextChoices(
        "StatusChoices", "SENT REQUEST ACCEPTED DECLINED CANCELLED"
    ) #look https://stackoverflow.com/questions/18676156/how-to-properly-use-the-choices-field-option-in-django for status

    status = models.CharField(blank=True, choices=StatusChoices.choices, max_length=255)

【讨论】:

    【解决方案2】:

    我学会了在 Django 中避免循环导入/关系的 1 个技巧是

    无论何时使用models.ForeignKey(),都写成

    models.ForeignKey('appname.modelname', related_name="related_name_of_your_pref")
    

    models.ForeignKey('modelname', related_name="related_name_of_your_pref")
    

    如果两个模型在同一个models.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      相关资源
      最近更新 更多