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

The Archive Base Latest Questions

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

I’m looking for the correct way to set full_name in SuperPerson instance. class Suffix(models.Mode):

  • 0

I’m looking for the correct way to set full_name in SuperPerson instance.

class Suffix(models.Mode):
    suffix = models.CharField(max_length=255)
    def __unicode__(self):
        return u'%s'%(self.suffix)

class Person(models.Model):
    first_name= models.CharField(max_length=255)
    last_name= models.CharField(max_length=255)
    suffixes= models.ManyToManyField(Suffix, blank=True, null=True)
    full_name= models.CharField(max_length=255)

class SuperPerson(Person):
    ignore_this_field= model.CharField(max_length=255)

full_name is hidden from the user on the Admin page, and is to be automatically be updated based on the other inputs on Admin page when the Admin page save button is hit.

I have tried overriding save like this and variations:

def save(self, *args, **kwargs):
    # Attempt to get data into the database so I can access it
    super(SuperPerson,self).save(*args,**kwargs)

    self.full_name = self.first_name + self.last_name 
    for suf in self.suffixes.all():
        self.full_name+= suf.__unicode__()

    # Now save the copy with full_name set as I wish
    super(SuperPerson,self).save(*args,**kwargs)

This method works if I hit the save button in the Admin page twice, which is unacceptable for my use cases, seems like the new self.suffixes I have entered from the Admin page hasn’t made it into database with the first super.save when I call self.suffixes.all().

I tried making full_name a property with decorator, but I also need to be able to filter Person and SuperPerson dbs using full_name, so that didn’t work, unless someone can tell me how to filter with a property. Though I would rather have the value saved in the db.

I tried pre_save and post_save signals – neither worked.

@receiver(pre_save, sender=SuperPerson)
def set_full_name(sender, instance, **kwargs):
    instance.full_name = instance.first_name + instance.last_name

    for suf in instance.suffixes.all():
        instance.full_name+= ', ' + suf.__unicode__()

Edit: – this has same effect – the instance suffixes do not match what was in the Admin page.

What is the right way to save full_name based on other inputs? Oh and I’m hoping to avoid messing with Admin forms.

ADDITIONAL INFORMATION:
It seems the problem is specifically that the suffixes field is not being updated by the time I’m trying to use it. I can update full_name to something else, like appending a string representing the current date, I just cannot access the suffixes.

Thanks, Dale

SOLUTION:

@receiver(m2m_changed, sender=Person.suffixes.through)
def set_full_name_after_ManyToMany_saved(sender, instance, **kwargs):
    instance.full_name = instance.first_name + instance.last_name
    for suf in instance.suffixes.all():
        instance.full_name+= ', ' + suf.__unicode__()
    print 'Saving As', instance.full_name
    instance.save()

I’m curious why I had to use Person.suffixes.through instead of SuperPerson, Suffixes or Person.suffixes – is there good documentation on this somewhere, I couldn’t find it. And, it runs the code 4 times, but at least ends up with the correct result in the final run.

Many thanks to Danny and burhan

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

    The problem is your m2m relationship with Suffix, or rather the way that django admin saves m2m relationships.

    A pretty good explanation is in this answer to Why is adding site to an object doesn't seem to work in a save() override in the Django admin?

    When you save a model via admin forms it’s not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is cleared and the new values set to whatever came out of the form.

    post_save() is actually still too early. That’s where the instance was saved, not its relationships.

    You need to connect to the m2m_changed signal: https://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed

    or wait for Django 1.4 where ModelAdmin gives you a “when all is done” signal:
    https://code.djangoproject.com/ticket/16115

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.