Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save a ManyToMany relationship from a ModelForm in Django
    primarykey
    data
    text
    <p>I'm not sure to save my ManyToMany relationship. I found my exact problem in this thread: <a href="https://stackoverflow.com/questions/5036142/django-embedded-manytomany-form">Django embedded ManyToMany form</a>, except instead of Sales and Products models, I have models that make up a movie.</p> <p>I tried the solution, but I receive a syntax error. I don't understand how Django should link the EquipmentModel, LightModel, and ActorModel to the ManyToMany relationship in MovieModel. So far (before trying the other thread's solution), the CharFields that are displayed on the form for LightModel, EquipmentModel, and ActorModel are not linked to the ManyToManyField in MovieModel. So when I save the forms and try to access a particular Movie's actors, all I see is a blank list. The solution from the other thread seems to make sense since it tries to link the models to the ManyToMany relationship in MovieModel, but I don't understand how Django knows which MovieModel to add to (how does it get the correct movieID?).</p> <p>On a side note, is there a way to check for duplicate movies when the user presses the 'Submit' button on the form? I want to avoid creating duplicates.</p> <p>views.py:</p> <pre><code>def add_movie(request, movieID=""): if request.method == "POST": form = MovieModelForm(request.POST) eform = EquipmentModelForm(request.POST) lform = LightModelForm(request.POST) aform = ActorModelForm(request.POST) print 'checking form' print request.POST.items() if form.is_valid() and eform.is_valid() and lform.is_valid() and aform.is_valid(): print 'form is valid' movie_to_add = form.save() e = eform.save() l = lform.save() a = aform.save() movie_to_add.actors.add(a) movie_to_add.lights.add(l) movie_to_add.equipments.add(e) # return HttpResponseRedirect('/data') else: # code for create forms .... return render_to_response('add_movie.html', {'form':form, 'eform':eform,'lform':lform, 'aform':aform,}, context_instance=RequestContext(request)) </code></pre> <p>Other code that may help:</p> <p>forms.py</p> <pre><code>class LightModelForm(forms.ModelForm): class Meta: model = LightModel class ActorModelForm(forms.ModelForm): class Meta: model = ActorModel class EquipmentModelForm(forms.ModelForm): class Meta: model = EquipmentModel class MovieModelForm(forms.ModelForm): class Meta: model = MovieModel fields = ("title", "rank") </code></pre> <p>models.py</p> <pre><code>class EquipmentModel(models.Model): equip = models.CharField(max_length=20) class ActorModel(models.Model): actor = models.CharField(max_length=20) class LightModel(models.Model): light = models.CharField(max_length=20) class MovieModel(models.Model): rank = models.DecimalField(max_digits=5000, decimal_places=3) title = models.CharField(max_length=20) equipments = models.ManyToManyField(EquipmentModel, blank=True, null=True) actors = models.ManyToManyField(ActorModel, blank=True, null=True) lights = models.ManyToManyField(LightModel, blank=True, null=True) def __str__(self): return self.title </code></pre> <p>Edit: removed unnecessary <strong>init</strong> and fields thanks to DTing</p> <p>Edit2: Fixed!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload