Django Admin - dynamic select options with foreignkey and manytomanyfields

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)
    

To try and summarize, is your basic question asking how you can nest Inlines within the Django Admin? Or have I misunderstood the issue you’re posting here?

If I am understanding you correctly, that is not something that the Django Admin app supports directly. Take a look at Documentation — django-nested-admin 3.2.4 documentation.

Let’s say there are two countries, each country has it’s own state. If I click on country “USA”, then it’s states should be filtered, and i should be able to click on it’s states like: Massachusetts

Ok, what you’re looking for is referred to as a “chain” or “cascade” dropdown list. This also isn’t something that is natively supported by the Django admin. The easiest way to handle this in the admin is to find a third-party package that will integrate into the admin.

I’d start with looking at some of the options listed in Django Packages : Search
(We’ve never tried to do this in the admin, so I don’t have any specific recommendations to make.)