Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:47:07+00:00 2026-05-27T06:47:07+00:00

I’m trying to do something that should be very common: add/edit a bunch of

  • 0

I’m trying to do something that should be very common: add/edit a bunch of related models in a single form. For example:

Visitor Details:
Select destinations and activities:
    Miami  []   -  swimming [], clubbing [], sunbathing[]
    Cancun []   -  swimming [], clubbing [], sunbathing[]

My models are Visitor, Destination and Activity, with Visitor having a ManyToMany field into Destination through an intermediary model, VisitorDestination, which has the details of the activities to be done on the destination (in itself a ManyToMany field into Activity).

Visitor ---->(M2M though VisitorDestination) -------------> Destination
                                            |
                       activities            ---->(M2M)---> Activity  

Note that I don’t want to enter new destination / activity values, just choose from those available in the db (but that’s a perfectly legit use of M2M fields right?)

To me this looks like an extremely common situation (a many to many relation with additional details which are a FK or M2M field into some other model), and this looks like the most sensible modelling, but please correct me if I’m wrong.

I’ve spent a few days searching Django docs / SO / googling but haven’t been able to work out how to deal with this. I tried several approaches:

  1. Custom Model form for Visitor, where I add multiple choice fields for Destination and Activity. That works ok if Destination and Activity could be selected independently, but here they are correlated, ie I want to choose one or several activities for each destination

  2. Using inlineformset_factory to generate the set of destination / activities forms, with inlineformset_factory(Destination, Visitor). This breaks, because Visitor has a M2M relation to Destination, rather than a FK.

  3. Customizing a plain formset, using formset_factory, eg DestinationActivityFormSet = formset_factory(DestinationActivityForm, extra=2). But how to design DestinationActivityForm? I haven’t explored this enough, but it doesn’t look very promising: I don’t want to type in the destination and a list of activities, I want a list of checkboxes with the labels set to the destination / activities I want to select, but the formset_factory would return a list of forms with identical labels.

I’m a complete newbie with django so maybe the solution is obvious, but I find that the documentation in this area is very weak – if anyone has some pointers to examples of use for forms / formsets that would be also helpful

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:47:08+00:00Added an answer on May 27, 2026 at 6:47 am

    In the end I opted for processing multiple forms within the same view, a Visitor model form for the visitor details, then a list of custom forms for each of the destinations.

    Processing multiple forms in the same view turned out to be simple enough (at least in this case, where there were no cross-field validation issues).

    I’m still surprised there is no built-in support for many to many relationships with an intermediary model, and looking around in the web I found no direct reference to it. I’ll post the code in case it helps anyone.

    First the custom forms:

    class VisitorForm(ModelForm):
        class Meta:
          model = Visitor
          exclude = ['destinations']
    
    class VisitorDestinationForm(Form):
        visited = forms.BooleanField(required=False)
        activities = forms.MultipleChoiceField(choices = [(obj.pk, obj.name) for obj in Activity.objects.all()], required=False, 
                                                          widget = CheckboxSelectMultipleInline(attrs={'style' : 'display:inline'}))
    
        def __init__(self, visitor, destination, visited,  *args, **kwargs):
            super(VisitorDestinationForm, self).__init__(*args, **kwargs)
            self.destination = destination
            self.fields['visited'].initial = visited
            self.fields['visited'].label= destination.destination
    
            # load initial choices for activities
            activities_initial = []
            try:
                visitorDestination_entry = VisitorDestination.objects.get(visitor=visitor, destination=destination)
                activities = visitorDestination_entry.activities.all()
                for activity in Activity.objects.all():
                    if activity in activities: 
                        activities_initial.append(activity.pk)
            except VisitorDestination.DoesNotExist:
                pass
            self.fields['activities'].initial = activities_initial
    

    I customize each form by passing a Visitor and Destination objects (and a ‘visited’ flag which is calculated outside for convenience)

    I use a boolean field to allow the user to select each destination. The field is called ‘visited’, however I set the label to the destination so it gets nicely displayed.

    The activities get handled by the usual MultipleChoiceField (I used I customized widget to get the checkboxes to display on a table, pretty simple but can post it if somebody needs that)

    Then the view code:

    def edit_visitor(request, pk):
        visitor_obj = Visitor.objects.get(pk=pk)
        visitorDestinations = visitor_obj.destinations.all()
        if request.method == 'POST':
            visitorForm = VisitorForm(request.POST, instance=visitor_obj)
    
            # set up the visitor destination forms
            destinationForms = []
            for destination in Destination.objects.all():
                visited = destination in visitorDestinations
                destinationForms.append(VisitorDestinationForm(visitor_obj, destination, visited, request.POST, prefix=destination.destination))
    
            if visitorForm.is_valid() and all([form.is_valid() for form in destinationForms]):
                visitor_obj = visitorForm.save()
                # clear any existing entries,
                visitor_obj.destinations.clear()
                for form in destinationForms:
                    if form.cleaned_data['visited']: 
                        visitorDestination_entry = VisitorDestination(visitor = visitor_obj, destination=form.destination)
                        visitorDestination_entry.save()
                        for activity_pk in form.cleaned_data['activities']: 
                            activity = Activity.objects.get(pk=activity_pk)
                            visitorDestination_entry.activities.add(activity)
                        print 'activities: %s' % visitorDestination_entry.activities.all()
                        visitorDestination_entry.save()
    
                success_url = reverse('visitor_detail', kwargs={'pk' : visitor_obj.pk})
                return HttpResponseRedirect(success_url)
        else:
            visitorForm = VisitorForm(instance=visitor_obj)
            # set up the visitor destination forms
            destinationForms = []
            for destination in Destination.objects.all():
                visited = destination in visitorDestinations
                destinationForms.append(VisitorDestinationForm(visitor_obj, destination, visited,  prefix=destination.destination))
    
        return render_to_response('testapp/edit_visitor.html', {'form': visitorForm, 'destinationForms' : destinationForms, 'visitor' : visitor_obj}, context_instance= RequestContext(request))
    

    I simply collect my destination forms in a list and pass this list to my template, so that it can iterate over them and display them. It works well as long as you don’t forget to pass a different prefix for each one in the constructor

    I’ll leave the question open for a few days in case some one has a cleaner method.

    Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.