Wednesday, January 26, 2022

Re: relations

Hi,  

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

On Wednesday, January 26, 2022 at 2:26:13 PM UTC-5 frank...@gmail.com wrote:
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-


On Wed, Jan 26, 2022 at 10:53 AM frank dilorenzo <frank...@gmail.com> wrote:
Thank you so much.  Have a great day!

frank-


On Wed, Jan 26, 2022 at 6:51 AM bnmng <benj...@bnmng.com> wrote:
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..)
On Monday, January 24, 2022 at 8:59:10 AM UTC-5 frank...@gmail.com wrote:
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.

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8f41caba-62f3-45e8-b4d6-e9584f9e8068n%40googlegroups.com.

No comments:

Post a Comment