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

The Archive Base Latest Questions

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

Schema Description A project’s status can change over time. In order to track the

  • 0

Schema Description

A project’s status can change over time. In order to track the status over time, I’ve created a many-to-many relationship between the Project model and the ProjectStatusType model through the ProjectStatus intermediary table.

While this allows tracking a project’s status over time, it increases the complexity of the schema such that retrieving the current status of a project or retrieving all open projects is more difficult.

enter image description here

Use Case

I want to be able to return all projects that are in a given state, such as all open projects. For instance, when users go to http://www.example.com/projects, I’d like only the open projects to be displayed in a table by default.

Questions

  1. Should I denormalize the schema and add a current_status field in the Project model?
  2. If I shouldn’t denormalize, what strategy should I use to retrieve the current status for each project? Should I create a property on the Project model that retrieves the current status?
  • 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-26T11:11:29+00:00Added an answer on May 26, 2026 at 11:11 am

    If you don’t need to search on it, I would create a property on the Project model. You can use the Max function to aggregate to get the record with the newest date.

    from django.db.models import Max
    
    class Project(models.Model):
        [...]
    
        @property
        def status_date(self):
            return self.projectstatus_set.aggregate(newest=Max('status_date'))['newest']
    

    This strategy is documented here.

    If you need to do lookups, then you should denormalize and add a field to Project. You can keep it current using signals. You would want to add a post_save listener to your ProjectStatus field, which would set its project’s date to the status’.

    from django.db.signals import post_save
    
    def update_status_date(sender, instance=None, **kwargs):
        project = instance.project
        project.status_date = max(project.status_date, instance.status_date)
        project.save()
    post_save.connect(update_status_date, sender=ProjectStatus)
    

    You can read more about signals here.

    ======

    EDIT: Since writing my original answer, the OP has clarified his question somewhat, and his clarification alters the example code for both of my strategies, although not their basic construction. I want to leave the original answer for those who may have needs more akin to the question I thought I was answering at the time.

    In my first example, he doesn’t really want the newest status_date itself, but rather the newest project status type. This would change the property substantially; you don’t need to use a MAX() SQL construct at all; you just want the first record attached to this object when ordered by date descending:

    class Project(models.Model):
        [...]
    
        @property
        def project_status(self):
            return self.status.order_by('-status_date')[0]
    

    The use cases around this are still the same. If you will always get a project first and then want to know its current status, this is the right way to go about it. If you need to index projects by status, then you need to denormalize. This is still best done through signals, but instead of saving the date like I was doing in my example above, you probably want to save a description. The principle remains the same, though.

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

Sidebar

Related Questions

So I recently created an HABTM relationship between two models (project & user). Before,
I have recreated this project in Eclipse as many ways as I can think
For kicks I'm writing a schema documentation tool that generates a description of the
Is a Star-Schema design essential to a data warehouse? Or can you do data
In my android project, i have saved camera snapshots along with some description in
Description Resource Path Location Type No grammar constraints (DTD or XML schema) detected for
My Project Model looks like this: # == Schema Information # Schema version: 20101117094659
I have flat file schema (description below), I am getting flat file(description below) through
I am using Propel as my DAL for my Symfony project. I can't seem
I need to query the following data from mongodb: Project has many Regions, a

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.