Dhanushka Amarakoon
Dhanushka Amarakoon

Reputation: 3762

Django select query with limited fields

I want to select a object with just one return field. I can do it using values. But the problem is when not using values it returns an object and when using values it returns a dictionary. Any reason for this difference. And is there a way I can get a return of objects with just one or two fields.

     obj=UserProfile.objects.get(pk=1)
     obj=UserProfile.objects.values('my_field').get(pk=1)

Upvotes: 0

Views: 77

Answers (1)

AyumuKasuga
AyumuKasuga

Reputation: 189

You can use only() method and enter fields which you need

obj=UserProfile.objects.only('my_field').get(pk=1)

Upvotes: 4

Related Questions