Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen and how is a many-to-many relationship created when saving a model?
    primarykey
    data
    text
    <p>I have a set of document objects and label objects, and I want those two objects to be linked. It's a typical many-to-many relationship. I have the following code:</p> <p>Models.py:</p> <pre><code>class Document(models.Model): title = models.CharField(max_length=50, unique=True) title_slug = models.SlugField(max_length=50, unique=True, editable=False) labels = models.ManyToManyField('Label') def save(self, *args, **kwargs): self.title_slug = slugify(self.title) super(Document, self).save(*args, **kwargs) 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(Document, self).save(*args, **kwargs) </code></pre> <p>Views.py:</p> <pre><code>class DocumentForm(ModelForm): class Meta: model = Document fields = ["title","labels"] def upload_document(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): new_document = form.save() return HttpResponseRedirect("/thanks/") else: form = DocumentForm() return render_to_response('upload_page.html', {'form':form}, context_instance=RequestContext(request)) </code></pre> <p>When I upload a document, it gets added to the database, however no labels are being created or associated with the document. Do I need to explicitly add something to the Document's save() function to make this happen? Or somewhere in the Views.py file? I'd imagine it'd go something like:</p> <ul> <li>Check to see if the label that's being added already exists</li> <li>If it doesn't, then create a new label</li> <li>Grab both the current document_id and the new/existing label_id</li> <li>Add a record to the document_labels table (automatically created for the many-to-many relationship)</li> </ul> <p>I feel like that's pretty standard functionality that I assumed would be built in to a many-to-many relationship in django, but it doesn't seem to be working for me so far. I'm trying to avoid reinventing the wheel here. Somewhat new to django.</p> <p>Thanks in advance!</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.
    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