Django Many to many field dependent upon another many to many field

I’m creating two many-to-many fields based on same model in a single model. I would like to show only those instances in second many-to-many field which are selected in first many to many field to further apply selection. Should I either create a child model in which the data is saved with selected many to many field instances as many to many field in child model in table or if any other way. which approach should i handle this behaviour with.

class Params(models.Model):
    name = models.CharField(max_length = 200)
    comments = Model.TextField()
    
    def __str__(self):
           return self.name

class Selection(models.Model):
    select_params = models.ManyToManyField(Params, blank=True, related_name = "selection_1")
    edit_selected_params = models.ManyToManyField(Params, blank=True, related_name = 
            "selection_from_selec_params")

Many thanks

When you say “show”, are you saying that:

  • you’re building a page with two multi-select fields
  • the first field may have one or more items already selected
  • the second field should only show those items selected in the first field
  • if the first field is updated (selections made or cleared), you want the second field updated as well - without doing a page refresh.

Is this what you’re looking to do?

Or, are you looking to do this across separate pages, where page one has the multi-selection box and page two is populated based on page one?

Hi KenWhitesell,

Yes absolutely similar to this, The second multi-select field should have all data selected in first field. The page can be refreshed or go further in the next step if I can make a child model if that is the right approach. or if both the multi select fields can be in same model. Any right approach

Thanks,

Side note: There isn’t any requirement for a direct correlation between your models and the forms displayed on pages. Your models, forms, and views are three separate and independent entities. You want to design your models to best represent the entities being modeled. You want your forms and templates to best present the UI/UX, and you want your views to tie the two together in the most coherent way possible.

Coming back to your original question then, if selection is some object of type Selection, then selection.select_params.all() is the set of Params related to selection. Those would be the objects that you would use as the “candidate” entries (the available choices) for the select list for edit_selected_params.

Thanks KenWhitesell, got it. I’m more of thinking in the prespective of admin panel as well. as to share admin panel with the client and the client wants to select the instances in many-to-many fields after that client further wants to filter the instances selected in many-to-many field from another many to many field. if this can be done or right

Not, generally-speaking, a good idea. See the first two paragraphs at The Django admin site | Django documentation | Django

As for the rest, sure it can be done. It’s just a matter of you identifying what items that need to be shown on each page through the process. You should be defining what each page is going to contain and what information needs to be passed through to the next stage of that process.