relations

49 views
Skip to first unread message

frank dilorenzo

unread,
Jan 24, 2022, 8:59:10 AM1/24/22
to Django users
I have tried several different ways but I cannot seem to get this right.  What I have is a list
of suppliers.  Each supplier can have many shipments and each shipment can have many species.  Seems simple enough but apparently I must be more simple.

I need a suggestion of how to relate these table.

a supplier can have many shipment.  A shipment can have many species.  Any help would be appreciated.  Thanks.

bnmng

unread,
Jan 26, 2022, 7:51:47 AM1/26/22
to Django users
I would start by defining Supplier in your models.py, then Shipment with a ForeignKey reference to Supplier

I'm assuming (forgive me if I'm wrong) that not only can a shipment have many species, but a species can be in many shipments, so if that's the case, the most obvious way is to go with ManyToMany for that relationship

https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/

class Supplier(models.Model):
    (etc..etc..)

class Shipment(models.Model):
    supplier = models.ForeignKey(
        Supplier,
        on_delete=models. (...etc.. etc...)

class Species(models.Model):
    shipment = models.ManyToManyField(
        Shipment,
        (etc..)

frank dilorenzo

unread,
Jan 26, 2022, 11:53:27 AM1/26/22
to django...@googlegroups.com
Thank you so much.  Have a great day!

frank-


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc667e81-ce32-4df5-8f88-47dff3d852c8n%40googlegroups.com.

frank dilorenzo

unread,
Jan 26, 2022, 2:26:13 PM1/26/22
to django...@googlegroups.com
After trying the suggestions I get these errors.

supplier.models:

class Supplier(models.Model):

    name = models.CharField(max_length=50)

    phone = models.CharField(max_length=15, null=True, blank=True)

    email = models.CharField(max_length=120, null=True, blank=True)

    country = models.CharField(max_length=120, null=True, blank=True)

    address = models.CharField(max_length=120, null=True, blank=True)

    city = models.CharField(max_length=120, null=True, blank=True)

    state = models.CharField(max_length=120, null=True, blank=True)

    zipCode = models.CharField(max_length=10, null=True, blank=True)


    def __str__(self):

        return self.name


shipment.models:

----------------


from django.db import models


from specie.models import Specie

from supplier.models import Supplier


# Create your models here.



class Shipment(models.Model):

    created = models.DateTimeField()

    specie = models.ManyToManyField(Specie)

    label = models.CharField(max_length=10)

    received = models.PositiveIntegerField()

    bad = models.PositiveIntegerField(default=0)

    non = models.PositiveIntegerField(default=0)

    doa = models.PositiveIntegerField(default=0)

    para = models.PositiveIntegerField(default=0)

    released = models.PositiveIntegerField(default=0)

    entered = models.BooleanField(default=False)

    supplier = models.ForeignKey(Supplier, on_delete=models.DO_NOTHING)


    def __str__(self):

        return self.supplier


    class Meta:

        ordering = ["label"]


    def __str__(self):

        return self.label


# =================================#

When I add the line supplier = models.ForeignKey(Supplier, on_delete=models.DO_NOTHING)

I get this error:


File "/Users/frankd/django_projects/Insectarium/src/shipment/models.py", line 4, in <module>

    from supplier.models import Supplier

  File "/Users/frankd/django_projects/Insectarium/src/supplier/models.py", line 3, in <module>

    from shipment.models import Shipment

ImportError: cannot import name 'Shipment' from partially initialized module 'shipment.models' (most likely due to a circular import) (/Users/frankd/django_



specie.models:

--------------

from django.db import models

from django.utils import timezone

from ckeditor.fields import RichTextField


from shipment.models import Shipment


# Create your models here.



class Specie(models.Model):

    scientific_name = models.CharField(max_length=50)

    common_name = models.CharField(max_length=50, blank=True, null=True)

    description = RichTextField(blank=True, null=True)

    image = models.ImageField(

        upload_to="specie/images/species", default="no_picture.png"

    )

    shipment = models.ManyToManyField(Shipment)

    created = models.DateField(default=timezone.now)


    def __str__(self):

        return self.scientific_name


    class Meta:

        ordering = [

            "scientific_name",

        ]


    def __str__(self):

        return self.scientific_name


# ====================== #

when I add the line shipment = models.ManyToManyField(Shipment)

I get this error.


File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed

  File "/Users/frankd/django_projects/Insectarium/src/shipment/models.py", line 3, in <module>

    from specie.models import Specie

  File "/Users/frankd/django_projects/Insectarium/src/specie/models.py", line 5, in <module>

    from shipment.models import Shipment

ImportError: cannot import name 'Shipment' from partially initialized module 'shipment.models' (most likely due to a circular import) (/Users/frankd/django_



I think I tried this before but couldn't resolve these errors. Any suggestions would be appreciated.



frank-

bnmng

unread,
Jan 26, 2022, 9:13:33 PM1/26/22
to Django users
Hi,  

You shouldn't have to import since the models are in the same models.py  

frank dilorenzo

unread,
Jan 27, 2022, 10:17:19 AM1/27/22
to django...@googlegroups.com
Well my friend, the only reason I have to do the imports is because each model is in it's own app. In every example I have seen the models are usually  small and contained in one app.
Perhaps I need to redesign my project to have everything lumped together?

In my project I have an app for base, shipment, supplier, species, and user.  Each app has its own rules of operations, meaning each has its own css, templates etc. 
I have looked at example until I am blue in the face but I cannot seem to get past the "cannot import name 'Supplier' from partially initialized module 'supplier.models' (most likely due to a circular import) (/Users/frankd/django" error.

I'm not sure what the "partially initialized" is telling me.

frank-


frank dilorenzo

unread,
Jan 27, 2022, 3:33:42 PM1/27/22
to django...@googlegroups.com
After numerous attempts I think I found my solution. (NO ERRORS)

supplier/models.py
------------------------

    shipment = models.ForeignKey(

        Shipment, on_delete=models.DO_NOTHING, null=True, blank=True)



specie/models.py 

----------------

no reference to any other model



supplier/models.py

-------------------

    specie = models.ManyToManyField(Specie)




Thanks for all the suggestions.


frank-

Reply all
Reply to author
Forward
0 new messages