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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:23:44+00:00 2026-06-12T09:23:44+00:00

I am fairly new to Python + Django and I am stuck with the

  • 0

I am fairly new to Python + Django and I am stuck with the following problem. I have created a custom ModelField like:

class MyField(models.TextField):

    def __init__(self, *args, **kwargs):
        super(MyField, self).__init__(*args, **kwargs)

    def pre_save(self, model_instance, add):
        # custom operations here
        # need access to variable xyz

The model using this field looks something like:

 class MyModel(models.Model):
     my_field = MyField()

     def __init__(self, model, xyz, *args, **kwargs):
         self.instance = model
         # how to pass xyz to ModelField before pre_save gets called?
         self.xyz = xyz 

     def save(self, *args, **kwargs):
         if self.instance:
             self.my_field = self.instance    

Q: Like it says in the comment, is there a way to pass a variable to the ModelField instance at runtime, ideally before my_field.pre_save() gets called?

  • 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-06-12T09:23:45+00:00Added an answer on June 12, 2026 at 9:23 am

    You don’t need to do anything to pass the xyz variable on — it is an instance variable on the model, so it is already present in the model_instance variable that gets passed to pre_save()

    class MyField(models.TextField):
    
        def pre_save(self, model_instance, add):
            ...
            # Access model_instance.xyz here
            ...
            # Call the superclass in case it has work to do
            return super(MyField, self).pre_save(model_instance, add)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Python and have a question regarding the following class: class
I'm just learning about python. I'm fairly new. I have the following code that
Still fairly new to django and python... I've defined two forms as follows: class
Still fairly new to django and python. I've defined two nearly identical models that
Simple one here but I'm fairly new to Python. I have a string like
I'm fairly new to both Django and Python. This is my first time using
I am fairly new to Python, so I welcome alternative approaches. I have a
I very new to Python, and fairly new to regex. (I have no Perl
I am fairly new to programming and to python and wxpython. I have looked
I'm fairly new to python and following along with part 4 of the tutorial

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.