2

I have created an admin user and two models

class Tcu:
    user = models.ForeignKey(User)
    imei = models.CharField(max_length=30, unique=True)

class Position:
    tcu = models.ForeignKey(Tcu)
    latitude = models.CharField(max_length=30)
    longitude = models.CharField(max_length=30)
    gps_date = models.CharField(max_length=20)
    speed = models.CharField(max_length=10, null=True, blank=True)
    heading = models.CharField(max_length=10, null=True, blank=True)

After that I manually assign my admin user to two TCUs.

The first TCU has three position data:

{"latitude": "21", "longitude": "21"}, {"latitude": "22", "longitude": "22"}, {"latitude": "23", "longitude": "23"}

The second TCU has two position data:

{"latitude": "10", "longitude": "10"}, {"latitude": "11", "longitude": "11"}

After that I create a view in order to get the last position of both TCUs.

def tcu_position(request):
    current_user_id = request.user.id
    tcu_pos = Position.objects.filter(tcu_id__user_id=current_user_id).values('latitude', 'longitude').order_by('-id')[:1:1]
    return JsonResponse ({'json_position_list': list(tcu_pos)})

The result is that I only get the last position of the second TCU:

{"latitude": "11", "longitude": "11"}

How can I get both last position from first and second TCU ?

2
  • Is [:1:1] intended? If not, then you should change it accordingly because it limits how many records you will get. Commented Nov 25, 2015 at 12:40
  • I know that I am getting the last position of the json object.. but I need the last position of each Tcu Commented Nov 25, 2015 at 12:41

1 Answer 1

5

If I understand correctly, you want the last Position for each Tcu belonging to the current user ? If yes the following should work:

positions = [
  tcu.position_set.order_by('-id').values('latitude','longitude')[0]   
  for tcu in request.user.tcu_set.prefetch_related('position_set')
  ]

Someone might prove me wrong but I don't think there's a simple way to get what you want without iterating on the Tcu set...

Edit: if you have Tcus without position, you may want to filter them out (preferably at the Queryset level) to avoid IndexError:

tcus =  request.user.tcu_set.exclude(position_set__isnull=True).prefetch_related('position_set')
positions = [
  tcu.position_set.order_by('-id').values('latitude','longitude')[0]   
  for tcu in tcus
  ]
Sign up to request clarification or add additional context in comments.

7 Comments

Right, I want the last position for each Tcu belonging to the current user!
Hi Bruno, this was working perfect but I notice a problem in my system when a Tcu exist without any position associated. Following the above example, if I add a third Tcu without a position I get this error: list index out of range.
I would be gratefull if you could help me
@picador that's fairly trivial and you should be able to fix this by yourself. You just have to filter out Tcu without related positions in request.user.tcu_set. You can filter either at the ORM level (cf Django's ORM doc for more), which should be the most efficient solution, or at the Python level using the optional conditional part of the List Comprehension (cf Python's list comprehension's doc for more).
thank you for your help I have checked the documentation and I add a conditional part to the List Comprehension. I try some if statments without exit like this one (if tcu.position_set is not None). As you said, I need filter out the tcu with none related positions. I've never had to do this before using Django, so I may have made a completely stupid question. But please could you help me with the if statment ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.