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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:39:06+00:00 2026-05-24T15:39:06+00:00

I have a Django model with 2 many-to-many fields. When the model is saved

  • 0

I have a Django model with 2 many-to-many fields. When the model is saved from the admin interface I need to check if the second field is blank and if it is blank then I need the items from the first fielded copied to the second. How can I do this?

UPDATE

Matthew’s answer seems like it would work great but I can’t get the instance to save after I copied the field. I have tried instance.save() without any success.

  • 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-24T15:39:07+00:00Added an answer on May 24, 2026 at 3:39 pm

    The signal to be use is not post_save, rather m2m_changed, that is sent much after the model is saved to the database.

    @models.signals.m2m_changed(sender=MyModel.second_m2m.through)
    def duplicate_other_on_this_if_empty(sender, instance, action, reverse, model, pk_set, **kwargs):
        # just before adding a possibly empty set in "second_m2m", check and populate.
        if action == 'pre_add' and not pk_set:
            instance.__was_empty = True
            pk_set.update(instance.first_m2m.values_list('pk', flat=True))
    
    @models.signals.m2m_changed(sender=MyModel.first_m2m.through)
    def duplicate_this_on_other_if_empty(sender, instance, action, reverse, model, pk_set, **kwargs):
        # Just in case the "first_m2m" signals are sent after the other
        # so the actual "population" of the "second_m2m" is wrong:
        if action == 'post_add' and not pk_set and getattr(instance, '__was_empty'):
            instance.second_m2m = list(pk_set)
            delattr(instance, '__was_empty')
    

    Edit: next code is simpler, and based upon new knowledge on the models definition

    In your code the ‘first_m2m’ signals are sent before the the ‘second_m2m’ (it really depends on your model definition). So we can work on the assumption that when the ‘second_m2m’ signals are received, the ‘first_m2m’ is already populated with current data.

    This makes us happier, because now you only need to check the m2m-pre-add:

    @models.signals.m2m_changed(sender=MyModel.second_m2m.through)
    def duplicate_other_on_this_if_empty(sender, instance, action, reverse, model, pk_set, **kwargs):
        # just before adding a possibly empty set in "second_m2m", check and populate.
        if action == 'pre_add' and not pk_set:
            pk_set.update(instance.first_m2m.values_list('pk', flat=True))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Django Model with many fields which user must fill. If I'll create
We have a django-model containing a many-to-many field. We use the same form to
I have a Django model with a large number of fields and 20000+ table
I have a Django form that uses an integer field to lookup a model
I have two models in Django: groups and entries. Groups has a many-to-many field
I have Django models setup in the following manner: model A has a one-to-many
I have several models inheriting from a base model. The fields in the base
I have a Django application that defines a one-to-many model relationship. The models look
Imagine, I have News models with many text fields class News(models.Model): title = models.CharField(max_length=255)
I'm building a Django site. I need to model many different product categories such

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.