Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:19:40+00:00 2026-05-26T14:19:40+00:00

I have a set of document objects and label objects, and I want those

  • 0

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:

Models.py:

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)

Views.py:

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))

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:

  • Check to see if the label that’s being added already exists
  • If it doesn’t, then create a new label
  • Grab both the current document_id and the new/existing label_id
  • Add a record to the document_labels table (automatically created for the many-to-many relationship)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:19:41+00:00Added an answer on May 26, 2026 at 2:19 pm

    As other people said, you cannot save in one-shot Document object and its ManyToMany field, because django create "intermediatary join table", which need the Document object’s ID, which is not defined at that point.

    There is a save_m2m function in ModelForm, that is supposed to be called by the Form itself, as described in the doc

    However, if it doesn’t work, maybe a trick is to call save_m2m in the view function, like this:

    def upload_document(request):
        if request.method == 'POST':
            form = DocumentForm(request.POST, request.FILES)
            if form.is_valid():
                new_document = form.save()
                form.save_m2m()
                return HttpResponseRedirect("/thanks/")
    
        else:
            form = DocumentForm()
    
        return render_to_response('upload_page.html', {'form':form}, context_instance=RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Car objects and using the following piece of code
I have a (set of) Word document(s) for which I'm trying to get various
I have set linebreak and wrap. The document looks great on my screen but
I have a set of Word documents which I want to publish using a
I have set up transactional replication between two SQL Servers on different ends of
Problem: I have a document class which contains a list of objects. These objects
If you have a document full of absolute positioned items and you set a
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have the following document called Reservation: { CustomerId: 1, Items: [ { EmployeeId:
I have the following template in django, i want to get the totals of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.