Here the supplier is related to Supplier model (ForeignKey) and substrate is with Substrate (ManyToManyField). The main model here is SupplierInfo from which we can filter suppliers and get their respective substrate.
When ever admin clicks on the dropdown of Arrival_supplier and, SupplierInfo_substrate should be filtered with the supplier name and should be able to receive SupplierInfo_substrate inside Arrival_substrate.
The Arrival model, where the filtered SupplierInfo_substrate should be saved.
class Arrival(models.Model):
submitted_on = DateTimeField(auto_created=True, null=True, blank=False, editable=False)
edited_on = DateTimeField(auto_now_add=True)
submitted_by = ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True)
supplier = ForeignKey(Supplier, null=True, on_delete=models.SET_NULL)
arrival_date = DateField(auto_now_add=True, null=True, blank=False)
substrate = ManyToManyField(Substrate, blank=True)
The Substrate and Supplier models, which are connected to Arrival and Supplier.
class Substrate(models.Model):
name = CharField(max_length=299, unique=True, null=True, blank=False)
class Supplier(models.Model):
name = CharField(max_length=299, unique=True, null=True, blank=False)
The SupplierInfo model, from which supplier and its substrate should be filtered.
Note: One supplier can have multiple substrates. Simply, filtering supplier returns multiple query set each with a different
substrate (ForeignKey)name.
class SupplierInfo(models.Model):
supplier = ForeignKey(to=Supplier, on_delete=models.SET_NULL, null=True, blank=False)
substrate = ForeignKey(to=Substrate, on_delete=models.SET_NULL,
null=True, blank=False)
mushroom = ForeignKey(to=Mushrooms, on_delete=models.SET_NULL,
null=True, blank=False)
weight = FloatField(null=True,
validators=[MinValueValidator(0.9)],blank=False)
yield_min = FloatField(null=True,
validators=[MinValueValidator(0.9), MaxValueValidator(100)])
yield_max = FloatField(null=True, blank=True,
validators=[MinValueValidator(0.9), MaxValueValidator(100)],
)
status = BooleanField(null=True)