1

I have models as below:

class ProjectRecord(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE, null=True, blank=True,related_name='user_projects')
    project = models.ForeignKey(Project,on_delete=models.CASCADE, null=True, blank=True, related_name='user_projects')
    project_type = models.CharField(max_length=30, choices=(("project_a","A"),("project_b","B")),null=True)
    version = models.FloatField(null=True, blank=True)

I want to filter the latest value of version with this: project = list(ProjectRecord.objects.filter(user=self.request.user, project_type='project_a')) , but I don't know how to achieve it.the data in database is similar as below:

id project_id version project_type
1   5          1.0      project_a
2   5          1.0      project_b
3   4          1.0      project_a
4   4          1.0      project_b
5   5          2.0      project_a          
6   5          2.0      project_b
7   5          3.0      project_a
8   5          3.0      project_b

For example, I want to get the latest value of project_id=5 to exact match other data and do not delete other project_id's value if their versions are not updated, the queryset should be display as below

id project_id version project_type
1   4          1.0      project_a
2   4          1.0      project_b
3   5          3.0      project_a
4   5          3.0      project_b

Thanks so much for any advice and assistance.

Elsa
  • 1
  • 1
  • 8
  • 27

3 Answers3

1

Or you can try with this:

from django.db.models import Max

queryset = ProjectRecord.objects.filter(user=self.request.user) \
                                .values('project_id', 'project_type') \
                                .annotate(max_version=Max('version')
  • Hi, I would like to use get_Foo_display in template, because project_type are choices, I need to display the detail of project_type, can I use other method to select Max version, because I can't use .values() and get_FOO_display in the meantime...... – Elsa Jul 02 '18 at 07:49
0

You could order_by('version') and then take latest object and watch if the second one have the same ver. so you take and it in and the third ...

  • 1
    Hi Ivan, thanks for your advice, I think order_by() is just a sort, values should be also display on pages. I want to display the latest version according to project ID, Thanks all the same. – Elsa Jun 09 '18 at 05:07
0

Try this,

from django.db.models import Max

queryset = ProjectRecord.objects.filter(user=self.request.user, project_type='project_a')
max_version = queryset.aggregate(max=Max('version')).get('max')
required_queryset = queryset.filter(version=max_version)


I'm not sure about the performance, but this will definitely work

JPG
  • 82,442
  • 19
  • 127
  • 206
  • 1
    Hi Jerin, thanks for your advice, I got a max value of project, but it lost something important values too, I also need other project_id's values, if their versions are not updated. Thanks again for your assistance, do you have any idea about this? – Elsa Jun 09 '18 at 05:05
  • hi peter, I would like to use `get_Foo_display` in template, because project_type are choices, I need to display the detail of `project_type`, can I use other method to select Max version, because I can't use .values() and get_FOO_display in the meantime...... – Elsa Jul 02 '18 at 07:49
  • I don't think it would be easy, anyway check this [SO answer](https://stackoverflow.com/a/21530804/8283848) – JPG Jul 02 '18 at 08:08
  • Still don't know how to use @property in my project, thank you all the same, I just would like to display the detail in template of `project_type`, it really doesn't have a simpler way to solving this? – Elsa Jul 02 '18 at 08:21