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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:51:25+00:00 2026-06-13T11:51:25+00:00

So I have the following model structure for a django project class RawData(models.Model): user

  • 0

So I have the following model structure for a django project

class RawData(models.Model):
    user = models.ForeignKey(User)
    timestamp = models.DateTimeField()
    ...
    value = models.PositiveIntegerField()

class SummarizedData(models.Model):
    SUMMARY_DURATION = (
        ('H', 'Hour'),
        ('D', 'Day'),
        ('M', 'Month'),
        ('Y', 'Year')
    )
    user = models.ForeignKey(User)
    duration = models.CharField(max_length=1, choices=SUMMARY_DURATION)
    timestamp = models.DateTimeField()      #for keeping track of which duration's data is this
    ...
    min_max_values = models.ManyToManyField(RawData)

Now my problem is related to min_max_values. I just want 2 RawData tuples per SummarizedData – one corresponding to the minimum RawData value seen for that duration, and the other for the maximum such value.

However since a particular RawData tuple could be the minimum value for more than one SummarizedData entries (for ex for a given hr, the same day etc), I related them through manytomany relation. But I am not able to figure out how to identify which of the 2 RawData values related to a SummarizedData is the max and which is the min unless I compare their respective values. So as of now I’m going with having functions in SummarizedData like getMaxValue and getMinValue.

My question is that for such a specific case, isn’t it possible to somehow include 2 fields in SummarizedData which each correspond to a single RawData field? Maybe like this?

min_value = models.OneToOneField(RawData)
max_value = models.OneToOneField(RawData)

Or should I just go with my first approach?

Edited

Sorry I should have used ForeignKey instead of OneToOneField

min_value = models.ForeignKey(RawData)
max_value = models.ForeignKey(RawData)
  • 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-13T11:51:26+00:00Added an answer on June 13, 2026 at 11:51 am

    I don’t think you should store the data as a ManyToMany field because of the following reasons:

    1. You will have to write explicit functions to determine which of the values is minimum and which is maximum
    2. Any aggregation operation (like average, sum, min, max) or sorting etc (if shown in tabular format) would be easier if you store minimum or maximum values separately.

    It will save a lot of extra calculations and you will need fewer lines of code to do whats required, so I would suggest you use separate ForeignKey fields for min_value and max_value

    min_value = models.ForeignKey(RawData)
    max_value = models.ForeignKey(RawData)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
Hi Stackoverflow people, in my current project, I have the following model structure: class
I have the following model structure class User(db.Model) : nickname = db.StringProperty(required=True) fullname =
I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =
I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
I have the following structure: Model public class EventEntry : LogEntry { public EventType
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following project structure: base project (service layer, model) web project webservice
I have two Django model classes that are structured similar to the following: class

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.