Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a whole lot of stuff going wrong here in addition to what spulec said.</p> <p>Your models.py look okay.</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>You don't need to override the <code>__init__</code> method on forms if you are not changing anything on init. You also don't need to be explicit about the fields if you want to include them all.</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>your view doesn't really make sense unless for every movie you are trying to add you also want to: </p> <ol> <li>add a new movie to the db using the submitted post data</li> <li>create one actor object and add to db</li> <li>create one light object and add to db</li> <li>create one equipment object and add to db </li> <li>take those three objects and add them to another movie's m2m relationships. <ul> <li>This other movie is some movie that you pulled from the urlconf and passed to your view, not the one you just created. </li> </ul></li> </ol> <p>This all seems a little strange.</p> <p>what i think you want to do is create all the equipment, actors and lights objects so they are in your db already, and use the default m2m formfield widget to select them when adding a movie.</p> <p>so:</p> <p>forms.py</p> <pre><code>class MovieModelForm(forms.ModelForm): class Meta: model = MovieModel </code></pre> <p>urls.py:</p> <pre><code>url(r'^add_movie/$', add_movie) </code></pre> <p>views.py:</p> <pre><code>def add_movie(request): if request.method=='POST': form = MovieModelForm(request.POST) if form.is_valid(): form.save() return HttpResponse('success') else: form = MovieModelForm() context = {'form':form } return render_to_response('some_template.html', context,context_instance=RequestContext(request)) </code></pre> <p>you could combine adding actors, lights, and equipment into the same form but that's a bit much for me to write out right now.</p> <p>As far as modifying your original code to add those lights, actors, and equipment to the movie you just created, you could do this:</p> <pre><code> if form.is_valid() and eform.is_valid() and lform.is_valid() and aform.is_valid(): new_movie = form.save() e = eform.save() l = lform.save() a = aform.save() new_movie.actors.add(a) new_movie.lights.add(l) new_movie.equipments.add(e) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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