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)
I don’t think you should store the data as a ManyToMany field because of the following reasons:
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