0

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.

AngBar
  • 73
  • 5

2 Answers2

2

This is what i would do:

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)
Henty
  • 603
  • 2
  • 7
0

1 trick I learned to avoid circular import/relationship in Django is

whenever using models.ForeignKey() write it as

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

or

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

if both models are in same models.py

Polymath
  • 328
  • 1
  • 9