I am currently working on a small project. Two models are of interest:
-Warehouse
-BetweenWarehouseTransfers
Note: each warehouse has multiple items in a different model (which are the products);
Question: I want to be able to transfer the items from one warehouse to another. To this end in BetweenWarehouseTransfers I would like to have one field specifying the warehouse to which I am transferring speak item (to_warehouse) and one field specifying from which warehouse this item is coming (from_warehouse). Later on I would like to add autocomplete_fields to these two fields which only accept existing warehouses as inputs.
Does anyone have an idea on how to create this circular relationship.
I have posted a couple of solutions which I envisioned but each time there is a circular relationship problem:
#0. (original version)
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')])
Thanks in advance.