michael93pl
michael93pl

Reputation: 347

Query db in Django for one row with a pair of values

I want to query DB in my views.py to retrieve one pair of values from two columns only. Let me show you my efforts:

Items.objects.filter(file_name=name).values('file_name', 'secret')

I need exactly one pair of values coming from columns called: 'file_name' and 'secret'. Value of 'secret' has to be in the same raw of 'file_name'

How could I write such query? What data type is it going to return?

Upvotes: 0

Views: 119

Answers (1)

Antoine Pinsard
Antoine Pinsard

Reputation: 34942

What do you mean "exactly one pair"? Do you want to retrieve the first pair of results?

Items.objects.filter(file_name=name).values_list('file_name', 'secret').first()

This will give you a tuple if there are results matching the query, None otherwise. Note that you would probably need to set an ordering to get consistent results.

Upvotes: 1

Related Questions