Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query an object using uuid in django

I am using uuid for creating an id field which is primary key as below

import uuid

class User_Profile(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

So whenever we save an object into the database it was saving as an UUID instance instead of string as below

user_profiles = User_Profile.objects.values_list('id', flat=True)
print user_profiles
[UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')]

now how to query it by using django ORM ? since it was not saving as a string i can't able to fetch it as below and getting an error

user_profile = User_Profile.objects.get(id='193b6acc-2b78-4ddc-9ef8-632cde33ef74')

error:

ValueError: invalid literal for int() with base 10: '193b6acc-2b78-4ddc-9ef8-632cde33ef74'

when i recieved this uuid as string from query params in Django, i also tried by converting it in to uuid from string as below and i got the error

import uuid
id = uuid.UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')
user_profile = User_Profile.objects.get(id=id)

error:

ProgrammingError: operator does not exist: uuid = numeric
LINE 1: ...ifications" FROM "tc_user_profile" WHERE "tc_user_profile"."id" = 33539211...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

So finally how to query a uuid id field from django database ?

like image 550
Shiva Krishna Bavandla Avatar asked May 22 '17 09:05

Shiva Krishna Bavandla


People also ask

What is the use of UUID in Django?

UUIDField is a special field to store universally unique identifiers. It uses Python's UUID class. UUID, Universal Unique Identifier, is a python library that helps in generating random objects of 128 bits as ids.

What is ID __ in in Django?

In Django, we can use the id__in query with a queryset to filter down a queryset based on a list of IDs. However, by default this will fail if your IDs are UUIDs.

What is use of __ str __ function in Django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface. Most of the time we name the display name which one could understand using self object.

What is __ GTE in Django?

The __lte lookup [Django-doc] means that you constrain the field that is should be less than or equal to the given value, whereas the __gte lookup [Django-doc] means that the field is greater than or equal to the given value.


2 Answers

Actually, I have tried this in my machine with database PostGres and it works,

>>> user = User_Profile.objects.create()
>>> user
<User_Profile: User_Profile object>
>>> user.id
UUID('d92c2280-4682-42ea-86c3-a1ed993c0638')
#copied the string of uuid only.
>>> id = 'd92c2280-4682-42ea-86c3-a1ed993c0638'
>>> User_Profile.objects.get(id=id)
<User_Profile: User_Profile object>

>>> import uuid
#converted the string into UUID format
>>> id = uuid.UUID(id)
>>> id
UUID('d92c2280-4682-42ea-86c3-a1ed993c0638')
>>> User_Profile.objects.get(id=id)
<User_Profile: User_Profile object>

If the problem still persists, try deleting the database and migrations and recreate it. The problem you are facing is not related to the database, it maybe something in your configuration.

like image 179
zaidfazil Avatar answered Oct 24 '22 19:10

zaidfazil


The field is a UUID so you should pass a UUID to query it. It is quite straight-forward if you see it:

import uuid
id = uuid.UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')
user_profile = User_Profile.objects.get(id=id)
like image 44
MichielB Avatar answered Oct 24 '22 20:10

MichielB