configuration

25 views
Skip to first unread message

frank dilorenzo

unread,
Jan 18, 2023, 5:56:47 PM1/18/23
to Django users
I cannot seem to resolve this configuration correctly.

I have a list of suppliers.
A supplier can have one profile
A supplier can have many shipments.
A shipment can have many species

I have tried several ways and I always end up with some kind of circular problem.
Any thoughts are appreciated.

Namanya Daniel

unread,
Jan 19, 2023, 9:05:42 AM1/19/23
to django...@googlegroups.com
Profile, shipments and species are models? Or your stored data in one model 

--
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/60f10b20-ddff-46b6-ac89-63756a192831n%40googlegroups.com.

Ryan Nowakowski

unread,
Jan 19, 2023, 10:41:32 AM1/19/23
to django...@googlegroups.com
Can you post what you've tried code-wise and any error messages you're seeing related to the circular problem?

ASAMOAH EMMANUEL

unread,
Jan 19, 2023, 6:44:35 PM1/19/23
to django...@googlegroups.com
In Django, you can set up your models to handle the many-to-many relationship between suppliers, shipments, and species using a `ManyToManyField` or a `ForeignKey` field.
Here's an example of how you could set up your models:

from django.db import models

class Supplier(models.Model):
    # supplier fields, such as name and contact information
    profile = models.OneToOneField(Profile, on_delete=models.CASCADE)
    shipments = models.ManyToManyField('Shipment', through='SupplierShipment')

class Shipment(models.Model):
    # shipment fields, such as date and shipping method
    species = models.ManyToManyField('Species', through='ShipmentSpecies')

class Species(models.Model):
    # species fields, such as name and description
   
class SupplierShipment(models.Model):
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    shipment = models.ForeignKey(Shipment, on_delete=models.CASCADE)
   
class ShipmentSpecies(models.Model):
    shipment = models.ForeignKey(Shipment, on_delete=models.CASCADE)
    species = models.ForeignKey(Species, on_delete=models.CASCADE)

Here, the `Supplier` model has a many-to-many relationship with the `Shipment` model through the `SupplierShipment` model, which acts as an intermediary. Similarly, the `Shipment` model has a many-to-many relationship with the `Species` model through the `ShipmentSpecies` model.
You can use the `Supplier.shipments.all()` to get all the shipments of a supplier, and `Shipment.species.all()` to get all the species of a shipment.
You can also set up the relationship using the `ManyToManyField` directly, but it is not recommended, as it will miss the ability to adding extra fields in the join table.
Please let me know if you have any questions or if this doesn't solve the problem you are facing.

--
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.
Reply all
Reply to author
Forward
0 new messages