我试图从用户模型中获取一个值,我的要求是or
条件应该在同一个查询中。
User.objects.get(
Q(premium_referral=form.cleaned_data.get('referral_code')) |
Q(id=form.cleaned_data.get('referral_code'))
)
但它给出了一个错误:
ValidationError at /register
['“XUSB5” is not a valid UUID.']
上面的查询对于id
是完美的,但是对于premium_referral
字段就不行了。如果我传递一个UUID,它就可以工作,但是如果我传递5个字符premium_referral
,它就失败了。
当我将它们分开时,Query也能很好地工作:
User.objects.get(premium_referral=form.cleaned_data.get('referral_code'))
User.objects.get(id=form.cleaned_data.get('referral_code'))
下面是模型:
class User(AbstractBaseUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
premium_referral = models.CharField(('Premium Referral Code'), max_length=30, null=True, blank=True, unique=True)
回溯
Internal Server Error: /register
Traceback (most recent call last):
File "env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2434, in to_python
return uuid.UUID(**{input_form: value})
File "/usr/lib/python3.8/uuid.py", line 171, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "apps/accounts/views.py", line 31, in register
referrer = User.objects.get(
File "env/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "env/lib/python3.8/site-packages/django/db/models/query.py", line 424, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "env/lib/python3.8/site-packages/django/db/models/query.py", line 941, in filter
return self._filter_or_exclude(False, args, kwargs)
File "env/lib/python3.8/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "env/lib/python3.8/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1393, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1265, in build_filter
return self._add_q(
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1347, in build_filter
condition = self.build_lookup(lookups, col, value)
File "env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1193, in build_lookup
lookup = lookup_class(lhs, rhs)
File "env/lib/python3.8/site-packages/django/db/models/lookups.py", line 25, in __init__
self.rhs = self.get_prep_lookup()
File "env/lib/python3.8/site-packages/django/db/models/lookups.py", line 77, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2418, in get_prep_value
return self.to_python(value)
File "env/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2436, in to_python
raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“XUSB5” is not a valid UUID.']
[19/Nov/2021 09:52:54] "POST /register HTTP/1.1" 500 154403
1条答案
按热度按时间2sbarzqh1#
发生这种情况的原因是因为
id
(或premium_referral
)是一个**UUIDField
**[Django-doc]。因此将XUSB5
作为代码传递没有多大意义,因为这是一个无效的UUID。您可以做的是检查它是否可以转换为UUID,从而使用以下内容进行过滤: