gentle
gentle

Reputation: 151

How to get the Ids from a django model

Hi I have two models named A and B. I want to get the ids from my model B and compare it with the id in model A (Foreign Key relation). I used the following method to get the ids a = B.objects.filter(b_id=object.id).values_list('id')

But a printed a result of [(82L,), (83L,), (84L,), (85L,)] instead of [82, 83 ,] etc. How can I get a flat list without the appended Ls?

Upvotes: 2

Views: 351

Answers (2)

Mike Covington
Mike Covington

Reputation: 2157

These numbers are long integers. As such, they are returned with an L suffix. There really is no letter 'L' after the number though. This has been removed in Python 3, since all integers are now treated as long integers. I don't believe what you are seeing is any cause for alarm.

Edit: See why-do-integers-in-database-row-tuple-have-an-l-suffix for more info.

Upvotes: 2

dhke
dhke

Reputation: 15388

values_list() by default returns the values grouped into tuples.

If you don't want that, use

values_list('id', flat=True)

Upvotes: 5

Related Questions