I am trying to figure out the best way to have an inline formset with a many to many relationship. Right now I am the relationship is through a intermediate model as shown here in the docs http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships.
It works but I am not sure the best way to do a formset for this. Right now I am using the modelformset_factory and looping through the results and doing a save on the intermediary model, but then it does not update it only inserts, so I need to manually check and then add the ID to the model instance and do a save. This is my ugly way of doing it but it just seems wrong. Thanks. def m2m_form(request,object_id=None): model = get_object_or_404(Model, pk=object_id) IntModelFormSet = modelformset_factory (Model,form=InterModel,can_order=True,can_delete=True,extra=0) if request.method == "POST": form = ModelForm(request.POST, instance=model) formset = IntModelFormSet (request.POST,queryset=OtherModel.objects.filter(model=model)) if form.is_valid() and formset.is_valid(): instance = form.save() for f in formset.ordered_forms: om_instance = OtherModel.objects.get (name=f.cleaned_data['name']) exists = InterModel.objects.filter (modle=model,othermodel=om_instance) if exists is None: inter_instance = InterModel(model=model, othermodel=om_instance, extra=f.cleaned_data['extra']) inter_instance.save() else: inter_instance = InterModel(id=exists[0].id, model = model, othermodel=om_instance, extra=t.cleaned_data['extra']) inter_instance.save() return HttpResponseRedirect('/') else: form = ModelForm(instance=model) formset = InterModelFormSet(queryset=OtherModel.objects.filter (model=model)) return render_to_response('form.html', {'form':form,'formset':formset}, context_instance=RequestContext (request)) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---