R
R
redduckrobot2016-03-04 17:30:02
Django
redduckrobot, 2016-03-04 17:30:02

How to make a filter by relay (by the latest added or in a given range)?

Hello, I have this model:

class Statistics(models.Model):
    date_scan = models.DateTimeField(blank=True, auto_now_add=True)
    rank = models.FloatField(blank=True)


class Works(models.Model):
    ...


class RelStat(models.Model):
    work = models.ForeignKey(Works, related_name='relstat')
    statistics = models.ForeignKey(Statistics, related_name='relstat')

I can’t find or deal with this task on my own:
You need to get Works, with the last date_scan (i.e. take data from the last scan) and sort them by rank. Tried through filter relstat.last, but how to access relstat. Last I didn't understand. And sorting rank through order_by('relstat.last.rank') also fails.
And a similar decision needs to be made, say, for the last 2-3+ scans, i.e. for the latest relstat.
Can you please tell me how to solve this problem only with filters. In principle, you can take everything as it is and only then break it through cycles and branches, but I would like to make one simple filter. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question