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

  • Home
  • SEARCH
  • 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 6021995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:47:41+00:00 2026-05-23T03:47:41+00:00

Is there a way to directly update a M2M relationship, other than by deleting

  • 0

Is there a way to directly update a M2M relationship, other than by deleting the old_object & then adding a new_object?

This is what I currently have to add a new object —

if 'Add School' in request.POST.values():     
    form = EducationForm(request.POST)
    if form.is_valid and request.POST['school']:
         school_object = form.save()
         profile.educations.add(school_object)
         profile.save()
         return redirect('edit_education')

And this what I’m trying to do —

if 'Save Changes' in request.POST.values():
    form = EducationForm(request.POST)
    if form.is_valid and request.POST['school']:
         new_school_object = form.save(commit=False)    
         old_school_object = Education.objects.get(id = request.post['id']) 
         # profile.educations.get(old_school_object).update(new_school_object) # ?
         profile.save()
         return redirect('edit_education')

And here are my models —

class Education(models.Model):
    school = models.CharField(max_length=100)
    class_year = models.IntegerField(max_length=4, blank=True, null=True, choices=YEAR)
    degree = models.CharField(max_length=100, blank=True, null=True)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    ...
    educations = models.ManyToManyField(Education)
  • 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-23T03:47:42+00:00Added an answer on May 23, 2026 at 3:47 am

    Education is probably something personal to one UserProfile, so you should use a ForeignKey instead of M2M:

    class UserProfile(models.Model):
        user = models.ForeignKey(User, unique=True)
        ...
    
    class Education(models.Model):
        user_profile = models.ForeignKey(UserProfile)
        school = models.CharField(max_length=100)
        class_year = models.IntegerField(max_length=4, blank=True, null=True, choices=YEAR)
        degree = models.CharField(max_length=100, blank=True, null=True)
    

    (and optionally use model formsets: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#model-formsets )

    If Education is actually shared between users, it should not be possible for one user to modify/update it – since other users are using it as well! consider users Alice and Bob, both learning for BSc in USC class of 2011. If Alice changes this to MA, Bob education will change as well!

    Another tip: In your templates use <input type="submit" name="save" value="..."/> and <input type="submit" name="add" value="..."/> and in your ifs check for the keys “save” or “add” instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to directly read a binary file into RAM? What I
Is there a way to directly write result returned from TSQL / stored procedure
Is there any way to directly access a field of class in the aspx
Is there a way to use records directly in erl? No matter what I
Is there a way to complie code directly into Native Code instead of MSIL
Is there a way to attach metadata directly to a grouping element () in
Is there a way to checkout a file directly into a specific pending changelist
Is there any way to get RoleId without get directly from DB?, I know
Is there any way to render ASP.net MVC controls or code directly within a
Is there a way to see what's been saved to NSUserDefaults directly? I'd like

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.