I'm just starting out in Django, and am having trouble setting up a model on the admin side.
Here is the situation. I have 12 centers, each with over 20 laboratories. I have a model class for publications. On the admin side, I want to be able to assign centers and laboratories to each publication. More than one center can be assigned to each publication, and within each center more that one laboratory could be assigned.
But, I do not want the laboratory field to list all the possible laboratories because there are too many. I would like the list seen in the laboratory field to only display laboratories in the centers that are chosen in the center field (and ideally, if more than one center is chosen, there will be separate lists to choose from for each)
Here are the models I have:
class Publications(models.Model):
center = models.ManyToManyField('Center')
laboratory = models.ManyToManyField('Laboratory')
class Center(models.Model):
name = models.CharField(max_length=255)
class Laboratory(models.Model):
name = models.CharField(max_length=255)
So, after making Admin classes (PublicationsAdmin(admin.ModelAdmin), CenterAdmin(admin.ModelAdmin), LaboratoryAdmin(admin.ModelAdmin)
) and registering them in admin.py, I can go to the admin page and add the centers and the laboratories and then when adding publications, I can choose the center(s) and choose the lab(s). But there are too many labs in the list for easy use. I want to only see the labs that belong to the chosen centers (and ideally if more than one center is chosen, separate laboratory lists or input boxes or windows or something).
is there a way to link them together by setting things up differently? Or is there a way to modify the logic in PublicationsAdmin(admin.ModelAdmin)
?