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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:17:50+00:00 2026-06-13T12:17:50+00:00

I have a model (lets call it Entity) that has an attribute (Attribute) that

  • 0

I have a model (lets call it Entity) that has an attribute (Attribute) that changes over time, but I want to keep a history of how that attribute changes in the database. I need to be able to filter my Entities by the current value of Attribute in its manager. But because Django (as far as I can tell) won’t let me do this in one query natively, I have created a database view that produces the latest value of Attribute for every Entity. So my model structure looks something like this:

class Entity(models.Model):
    def set_attribute(self, value):
        self.attribute_history.create(value=value)

    def is_attribute_positive(self, value):
        return self.attribute.value > 0

class AttributeEntry(models.Model):
    entity = models.ForeignKey(Entity, related_name='attribute_history')
    value = models.IntegerField()
    time = models.DateTimeField(auto_now_add=True)

class AttributeView(models.Model)
    id = models.IntegerField(primary_key=True, db_column='id', 
        on_delete=models.DO_NOTHING)
    entity = models.OneToOneField(Entity, related_name='attribute')
    value = models.IntegerField()
    time = models.DateTimeField()

    class Meta:
        managed = False

My database has the view that produces the current attribute, created with SQL like this:

CREATE VIEW myapp_attributeview AS
SELECT h1.*
FROM myapp_attributehistory h1
LEFT OUTER JOIN myapp_attributehistory h2
    ON h1.entity_id = h2.entity_id
        AND (h1.time < h2.time
        OR h1.time = h2.time
        AND h1.id < h2.id)
WHERE h2.id IS NULL;

My problem is that if I set the attribute on a model object using set_attribute() checking it with is_attribute_positive() doesn’t always work, because Django may be caching that the related AttributeView object. How I can I make Django update its model, at the very least by requerying the view? Can I mark the attribute property as dirty somehow?

PS: the whole reason I’m doing this is so I can do things like Entity.objects.filter(attribute__value__exact=...).filter(...), so if someone knows an easier way to get that functionality, such an answer will be accepted, too!

  • 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-13T12:17:51+00:00Added an answer on June 13, 2026 at 12:17 pm

    I never directly solved the problem, but I was able to sidestep it by changing is_attribute_positiive() to directly query the database table, instead of the view.

    def is_attribute_positive(self, value):
        return self.attribute_history.latest().value > 0
    

    So while the view gives me the flexibility of being able to filter queries on Entity, it seems the best thing to do once the object is received is to operate directly on the table-backed model.

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

Sidebar

Related Questions

I have an entity, let's call it CommonEntity that has a primary key used
Lets say I have three django model classes - lets call them A, B
I have a model whereby I have an abstract class (lets call it Vehicle
In a webapp, i have a data-model (let's call it Item). The data of
Lets say I have model inheritance set up in the way defined below. class
Lets say a have the following Model public class FirstModel { public List<SecondModel> SecondModels
So for example ... Lets say I have a Posts model and a News
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()

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.