0

I'm currently working with a couple of models that have a one to many relationship.

I've managed to access all the attributes/foreign key attributes in my template files using template tags and one.many_set.all etc.

class One(models.Model)
  name = models.CharField(max_length=50)

class Many(models.Model)
  name = models.CharField(max_length=50)

  related = models.ForeignKey(One, null=True, blank=True)

  def __str__(self): 
    return self.name     

What I'd like to do is split them up, so for example, have a drop down list for the 'One' model, which, on change, updates another list with it's related 'Many' objects, which I would then work with.

I've looked around and there doesn't seem to be anything I can find which specifically helps. Any advice would be much appreciated.

Thanks

Nowandthen98
  • 300
  • 1
  • 3
  • 13
  • See if it helps: 1) https://www.devinterface.com/it/blog/how-to-implement-two-dropdowns-dependent-on-each-other-using-django-and-jquery 2) http://stackoverflow.com/questions/29458604/create-django-dependent-dropdown-list-doesnot-autopopulate-second-dropdown – planet260 Apr 04 '17 at 11:00

1 Answers1

0

You could use the prefetch_related() method that Django provides (https://docs.djangoproject.com/en/1.10/ref/models/querysets/) to prepopulate your One model with the Many model. Next in the template you can use a JS that will filter out the records of Many depending on the One model.