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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:53:39+00:00 2026-05-27T21:53:39+00:00

Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(…) mentions

  • 0

Suppose I have this model (not real code):

class Message(models.Model):
    content = models.CharField(...)
    mentions = models.ManyToManyField(User)

content can for instance be:

Say hi to @Adam and @Bernard

During the Message model save(), “Adam” and “Bernard” are extracted from the text, and their respective user instances added to mentions, so that

 >>> m = Message.objects.create(content="Say hi to @Adam and @Bernard")
 >>> m.mentions.all()
 >>> [User: Adam, User: Bernard]

Now, as to my problem:

How do I cope with Bernard performing a sex-change and renaming himself Bernarda? I would very much like the content then to read Say hi to @Adam and @Bernarda.

Basically, I’ve tried a strategy of replacing names with ID’s when saving (Say hi to @Adam and @Bernard then becomes Say hi to @1 and @2, i.e. their User.pk’s). This works fine when saving, and OK-ish when reading.

Doing this replacement in the model’s save() method works fine, but read and lookup operations are a major problem. I cannot find an easy way of making sure that what is stored in the database (ie Say hi to @1 and @2) ‘magically’ becomes Say hi to @Adam and @Bernard whenever it is being queried (be that through .filter() and related operations) or its instance being evaluated.

I’m unsure whether I’m on the right track here; any pointers or ideas to doing this differently are very welcome!

  • 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-27T21:53:39+00:00Added an answer on May 27, 2026 at 9:53 pm
    1. Write a custom model field class, that extends CharField, you should overread the documentation first so that you have an idea of what’s there,

    2. that metaclass = models.SubfieldBase is required for the custom Field to call the custom to_python method

    3. Override to_python(), it takes a database value and should return a python value, there you can replace @1 by the username of User #1 (say @Adam or @Bernarda),

    4. Override get_prep_value(), it takes a python value and should return a value for the SQL, there you can replace @Adam by the pk of User Admin (say @1),

    5. Query on mentions, for example Message.objects.filter(mention=someuser), or make another field: one that contains the replaced version and one that contains the possibly out of date usernames – i don’t recommend the latter

    6. You could also query on content, for example Message.objects.filter(content__contains='@1')

    Voodoo doesn’t do any good in general, i would make a model method get_content_display() that does the replacement, and a templatetag that displays the html version of the content – since your probably want to link @Adam to his user page anyway. I would stick to this because that’s KISS.

    CREDITS to LaundroMat for debugging this answer and fixing it (there was 2 errors and 1 inaccuracy, which he debugged and came back to report !!!). Also DO UPVOTE HIS COMMENT unless you have better way to reward him. Rock’on.

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

Sidebar

Related Questions

I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Suppose I have this simple app working : # model class Project has_many :numbers
Suppose that I have the model Foo in GAE and this query: query =
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
suppose we have a model in django defined as follows: class Literal: name =
I'm investigating on how validates_presence_of actually works. Suppose I have two models class Project
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to

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.