[Solved]-Query an object using uuid in django

6👍

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.

2👍

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)

0👍

i assume your using postgreSQL :

The Django docs say : When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32).

https://docs.djangoproject.com/fr/1.11/ref/models/fields/

Leave a comment