Search code examples
pythondjangodjango-ormuuid

Django self.model.DoesNotExist


I have a user with a field set as None:

None|Default User|07/07/2023

However, Django for some reason does not see it:

queryset=Q(id=UUID(User.objects.get(user_id=None)
...
raise self.model.DoesNotExist(pages.models.User.DoesNotExist: User matching query does not exist

The closing parentheses are present.

Setting the default field as another UUID present in the database as well isn't of any help. I am trying to run the migrations.

My models:

class User(models.Model):

"""
A class presenting user who can view lessons and have access to them
"""

user_id = models.UUIDField(
    primary_key=True,
    default=uuid.uuid4,
    editable=False,
    null=True
    )
name = models.CharField(max_length=60, default='Name')
date_of_registration = models.TimeField(default=timezone.now)

def __str__(self):
    return self.id

Setting the default to a present user_id doesn't help as well. Similar questions on stackoverflow were left without answers, no information about this particular problem in the Internet was found. What additional information should I provide?


Solution

  • The UUID is not NULL, so user_id=None will not work, it is the string None, so you retrieve this with:

    User.objects.get(user_id='None')

    A UUID is on most databases just a CharField that stores the UUID as a string, so it will call str(…) on it, and apparently it did that on the None object.