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 7160837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:27:51+00:00 2026-05-28T13:27:51+00:00

I have found this code http://djangosnippets.org/snippets/2283/ but I think it doesn’t work with manytomanyfields

  • 0

I have found this code http://djangosnippets.org/snippets/2283/ but I think it doesn’t work with manytomanyfields which use another model (keyword through) – I get an AttributeError when trying to merge.

Do you know any way to fix this or another method for merging objects ?

Edit: more details

I have 3 models: A, B, C

A has a manytomany field “m2mfield” pointing to C through B.

When I run the code from django snippets, it fails with the exception

'ManyRelatedManager' object has no attribute 'remove'

I think this has something to do with a comment in Django source (django.db.models.fields.related.py line 499) which says:

# If the ManyToMany relation has an intermediary model,
# the add and remove methods do not exist.

I think the code snippet I got doesn’t make any difference between ManyToMany relations with and without intermediary model. That’s why I’m looking for some way to fix that code or another way to achieve what I want (merging).

  • 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-28T13:27:52+00:00Added an answer on May 28, 2026 at 1:27 pm

    I eventually modified the code to handle the case of ManyToMany fields created ‘through’ a model.
    Here is what is to be modified:

    # Migrate all many to many references from alias object to primary object.
    for related_many_object in alias_object._meta.get_all_related_many_to_many_objects():
        alias_varname = related_many_object.get_accessor_name()
        obj_varname = related_many_object.field.name
    
        # Treatment depends on if the many_to_many field is created through another model
        if getattr(alias_object, alias_varname).through._meta.auto_created:
            if alias_varname is not None:
                # standard case
                related_many_objects = getattr(alias_object, alias_varname).all()
            else:
                # special case, symmetrical relation, no reverse accessor
                related_many_objects = getattr(alias_object, obj_varname).all()
            for obj in related_many_objects.all():
                getattr(obj, obj_varname).remove(alias_object)
                getattr(obj, obj_varname).add(primary_object)
        else:
            related_many_objects = getattr(alias_object, alias_varname).all()
    
            through_model = getattr(alias_object, alias_varname).through
            through_field_name = None
            for f in through_model._meta.fields:
                if isinstance(f, ForeignKey):
                    if f.rel.to == primary_class :
                    # f is the field in our 'through' model which points to an instance of primary_class
                        through_field_name = f.name
    
            for obj in related_many_objects.all():
                kwargs = {
                    through_field_name: obj,
                }
                for through_obj in through_model.objects.filter(**kwargs):
                    setattr(through_obj, through_field_name, primary_object)
                    through_obj.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this code sample import org.apache.http.params.CoreProtocolPNames; import org.apache.http.util.EntityUtils; public class PostFile {
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I have found myself doing this in my code to 'cache' the work done
VS 2008 I have this code snippet I found on a VB website. But
I found this code on the website http://rosettacode.org/wiki/Closest-pair_problem and I adopted the C# version
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
I found this PHP code in an app I have to modify... $links =
Hi i have problem with this code, i found it on the internet and
i have a problem using the Catch Clipboard Events code found on this link
I have this bit of code I found on the web at the end

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.