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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:04:16+00:00 2026-06-04T22:04:16+00:00

How do I create a dynamic field on a model? Let’s say I’m writing

  • 0

How do I create a dynamic field on a model?

Let’s say I’m writing an application related to the stock market. I make a purchase on one day and sometime later I want to check the gain (or loss) based on today’s price. I’d have a model like this:

class Purchase(models.Model):
  ticker = models.CharField(max_length=5)
  date = models.DateField()
  price = models.DecimalField(max_digits=20, decimal_places=3)
  quantity = models.IntegerField()

What I’d like to do is define a model something like this:

class PurchaseGain(Purchase):
  gain = models.DecimalField(max_digits=20, decimal_places=3)
  class Meta:
    proxy = True

So that I could do this:

todays_price = get_price_from_webservice(ticker)
for p in PurchaseGain.objects.get_purchase_gain(todays_price):
  print '%s bought on %s for a gain of %s' % (p.ticker, p.date, p.gain)

Where p.gain is dynamically computed based on the input to get_purchase_gain. Rather than just constructing dictionaries on the fly I want to use a model, because I’d like to pass this around and generate forms, save changes, etc from the instance.

I tried creating a derived QuerySet, but that led to a circular dependency, because Purchase needed to know about the QuerySet (through a custom manager) and the QuerySet returned an iterator that needed to instantiate a PurchaseGain, which was derived from Purchase.

What options do I have?

Thanks,
Craig

  • 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-04T22:04:19+00:00Added an answer on June 4, 2026 at 10:04 pm

    Creating a proxy class is what confused me. By just adding attributes to a Purchase, I was able to accomplish what I wanted.

    class PurchaseQuerySet(QuerySet):
      def __init__(self, *args, **kwargs):
        super(PurchaseQuerySet, self).__init__(*args, **kwargs)
        self.todays_price = None
    
      def get_with_todays_price(self, todays_price):
        self.todays_price = todays_price
        cloned = self.all()
        cloned.todays_price = todays_price
        return cloned
    
      def iterator(self):
        for p in super(PurchaseQuerySet, self).iterator():
          p.todays_price = self.todays_price
          yield p
    
    class PurchaseManager(models.Manager):
      def get_query_set(self):
        return PurchaseQuerySet(self.model)
    
      def __getattr__(self, name)
        return getattr(self.get_query_set(), name)
    
    class Purchase(models.Model):
      ticker = models.CharField(max_length=5)
      date = models.DateField()
      price = models.DecimalField(max_digits=20, decimal_places=3)
      quantity = models.IntegerField()
    
      objects = PurchaseManager()
    
      @property
      def gain(self):
        return self.todays_price - self.price
    

    Now I can do:

    for p in Purchase.objects.filter(ticker=ticker).get_with_todays_price(100):
      print p
      print p.gain
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using powerplot to create a dynamic graph in iphone. while going through http://www.field-theory.org/articles/powerplot/example.html
How does one create Dynamic CSS and JavaScript On-The-Fly (using PHP). This needs to
I'm having some trouble trying to understand how to create a dynamic choice field
I'm planning a WPF application which will be able to create dynamic data entry
I've been reading up on Dynamic Models and have been trying to create one.
I want to create some dynamic field in Jira similar to what we do
I just wanna ask if there's a way in C#/ASP.NET to create 'dynamic' tables/fields
I create dynamic LinkButton and I add LinkButton's Click Trigger to UpdatePanel. Now, When
I want to create dynamic conditional statement in java following are my expression in
I would like to create dynamic table in android (custom number of rows and

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.