Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your labels in the Document model is a M2M field, so that's going to end up rendering a multi-select in the rendered form (showing all the labels available in the system).</p> <p>Assuming that's what you want,</p> <p>in <strong>views.py</strong></p> <pre><code>def upload_document(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): labels = request.POST.getlist('labels') new_document = form.save() for label_id in labels: # we're only going to add currently defined labels label = Label.objects.get(id=int(label_id)) new_document.labels.add(label) new_document.save() return HttpResponseRedirect("/thanks/") else: form = DocumentForm() return render_to_response('doc_form.html', {'form':form}, context_instance=RequestContext(request)) </code></pre> <p>I updated the model for Label in <strong>models.py</strong>,</p> <pre><code>class Label(models.Model): name = models.CharField(max_length=40, unique=True) slug = models.SlugField(max_length=40, unique=True, editable=False) def save(self, *args, **kwargs): self.slug = slugify(self.name) super(Label, self).save(*args, **kwargs) def __unicode__(self): return self.name </code></pre> <p>If you were thinking of having the user also create labels on the fly, you need to override the labels field in your form with something else, like an input field. As an example, if you're instructing the users to enter labels seperated by commas, then you'll have an updated views.py like,</p> <pre><code>for label in labels: # labels entered by user try: lbl = Label.objects.get(name='label') except Label.DoesNotExist: lbl = None if not lbl: lbl = Label() lbl.name = label lbl.save() newDoc.labels.add(lbl) newDoc.save() </code></pre> <p>Hope this solves your problem or give you something to work from.</p>
    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. 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.
    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