0

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)?

anp925
  • 59
  • 4

1 Answers1

-1

Best option for this purpose is to use django-autocomplete-light. You will especially be interested in the forwarding feature, which does precisely what you want.

Also note that in order to filter centers belonging to a laboratory, you will need a foreign key from Center to Laboratory.

class Center(models.Model):
    name = models.CharField(max_length=255)
    laboratory = models.ForeignKey(Laboratory, models.PROTECT)
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • aha, I was thinking that might be the case. but, should it say Laboratory on the 2nd line instead of Center?I will check out the the autocomplete-light link. – anp925 Nov 20 '20 at 15:11
  • Yes indeed. My mistake. – Antoine Pinsard Nov 20 '20 at 15:12
  • EDIT it seems like autocomplete-light could work, but my understanding is that it's used in a form and view, but not on the admin page. but it'll take me some time to figure it out (their explanations aren't wonderful). In the mean time, maybe someone knows another way or another package. – anp925 Nov 20 '20 at 15:34
  • It works perfectly with admin page. Once you have customized your `Form` class, just set the `form` attribute of your `ModelAdmin`: https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#using-autocompletes-in-the-admin – Antoine Pinsard Nov 20 '20 at 15:41
  • i totally missed that. thanks. once I figure it out, i'll mark this as answered. it's taking me a while though – anp925 Nov 24 '20 at 07:18