Guest User

Untitled

a guest
Dec 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class Airport(models.Model):
  2. iata = models.CharField()
  3. name = models.CharField()
  4. latitude = models.FloatField()
  5. longitude = models.FloatField()
  6.  
  7. class Flight(models.Model):
  8. origin = models.ForeignKey('Airport', related_name='origins')
  9. destination = models.ForeignKey('Airport', related_name='destinations')
  10. owner = models.ForeignKey(User)
  11.  
  12. [LHR, id__count=2}, {CDG, id__count=2}, {LAX, id__count=1}, {JFK, id__count=1}]
  13.  
  14. Airport.objects.filter(
  15. Q(origins__owner=user) | Q(destinations__owner=user)
  16. )
  17. .distinct()
  18. .annotate(
  19. id__count=Count('origins', distinct=True) + Count('destinations', distinct=True)
  20. ).order_by('-id__count')
Add Comment
Please, Sign In to add comment