ValidationError - Django Q Query - is not a valid UUID - django

I am trying to get a value from the User Model, my requirement is that or condition should be in same query.
User.objects.get(
Q(premium_referral=form.cleaned_data.get('referral_code')) |
Q(id=form.cleaned_data.get('referral_code'))
)
But it gives an error:
ValidationError at /register
['“XUSB5” is not a valid UUID.']
The above query works perfect for id but not for premium_referral field. If I pass a UUID, it works, but if I pass 5 char premium_referral, then it fails.
Query also works perfectly when I separate them:
User.objects.get(premium_referral=form.cleaned_data.get('referral_code'))
User.objects.get(id=form.cleaned_data.get('referral_code'))
Below is the model:
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)
Traceback
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

The reason this happens is because the id (or the premium_referral) is a UUIDField [Django-doc]. It thus does not make much sense to pass XUSB5 as code, since that is an invalid UUID).
What you can do is check if it can be converted to a UUID and thus filter with:
from uuid import UUID
query = form.cleaned_data.get('referral_code')
qobj = Q(premium_referral=query)
try:
qobj |= Q(id=UUID(query))
except ValueError:
pass
User.objects.get(
qobj
)

Related

Django Model with custom string id field not being deleted

I have a model with a custom string for an id.
class BaseModel(Model):
id = CharField(
max_length=23,
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
default=generate,
editable=False,
)
When deleting, I am getting this error.
Internal Server Error: /admin/sampleapp/user/
Traceback (most recent call last):
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1988, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'AC7EEKrEhYZ9p1r-Q'
The above exception was the direct cause of the following exception:
response = func(self, request, queryset)
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\actions.py", line 39, in delete_selected
) = modeladmin.get_deleted_objects(queryset, request)
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\options.py", line 2099, in get_deleted_objects
return get_deleted_objects(objs, request, self.admin_site)
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 120, in get_deleted_objects
collector.collect(objs)
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 186, in collect
return super().collect(objs, source_attr=source_attr, **kwargs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 343, in collect
field.remote_field.on_delete(self, field, sub_objs, self.using)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 23, in CASCADE
collector.collect(
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 186, in collect
return super().collect(objs, source_attr=source_attr, **kwargs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 365, in collect
sub_objs = field.bulk_related_objects(new_objs, self.using)
File "path\to\sampleapp\venv\lib\site-packages\django\contrib\contenttypes\fields.py", line 524, in bulk_related_objects
return self.remote_field.model._base_manager.db_manager(using).filter(
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1502, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1448, in build_filter
condition = self.build_lookup(lookups, col, value)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1273, in build_lookup
lookup = lookup_class(lhs, rhs)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\lookups.py", line 276, in get_prep_lookup
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
File "path\to\sampleapp\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1990, in get_prep_value
raise e.__class__(
ValueError: Field 'object_pk' expected a number but got 'AC7EEKrEhYZ9p1r-Q'.
"POST /admin/sampleapp/user/ HTTP/1.1" 500 213503
Kindly help in solving this issue.
I first noticed this issue when attempting to delete via admin but that didn't work. Upon inspection, I noticed the issue was that the model could not be deleted even via shell.
From your traceback:
ValueError: invalid literal for int() with base 10: 'AC7EEKrEhYZ9p1r-Q'
It's expecting an integer.
Is there a particular reason you want to replace the id field? You could always just create a separate field for your custom one and use that as the id elsewhere in your program

AttributeError: 'Seekerskillset' object has no attribute 'skill_name'

I am trying to implement bulk_create my inserting multiple objects in
a relation, not sure whether I am doing it right i have added trace back as well below is skill set model
class Skillset(models.Model):
skill_name = models.CharField(max_length=255)
def __str__(self):
return self.skill_name
my view
skill_name = request.POST.getlist('skill_name')
skill_level = request.POST.getlist('skill_level')
print(f'skill name-> {skill_name} skill level ->{skill_level}')
seeker_skll = []
# testing destructing
for skill_nme, skill_lvl in zip(skill_name, skill_level):
skill_set = Skillset.objects.get(skill_name=skill_nme)
seeker_skll.append(Seekerskillset(
skill_set=skill_set, skill_level=skill_lvl, seeker=user))
seeker_skll = Skillset.objects.bulk_create(seeker_skll)
print(seeker_skll)
return redirect('/users/dashboard')
Model
class Seekerskillset(models.Model):
skill_set = models.ForeignKey(Skillset, on_delete=models.CASCADE)
seeker = models.ForeignKey(SeekerProfile, on_delete=models.CASCADE)
skill_level = models.CharField(max_length=25)
class Meta:
verbose_name = 'Seeker skill set'
error i am getting
AttributeError: 'Seekerskillset' object has no attribute 'skill_name'
Traceback (most recent call last):
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\atif\PycharmProjects\my_proj\mysite_jobportal\seekerbuilder\views.py", line 43, in update_details
seeker_skll = Skillset.objects.bulk_create(seeker_skll)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 515, in bulk_create
objs_without_pk, fields, batch_size, ignore_conflicts=ignore_conflicts,
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1293, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1360, in as_sql
for obj in self.query.objs
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1360, in <listcomp>
for obj in self.query.objs
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\Users\atif\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 822, in pre_save
return getattr(model_instance, self.attname)
Exception Type: AttributeError at /users/app_det/
Exception Value: 'Seekerskillset' object has no attribute 'skill_name'
The problem is caused by using Skillset to bulk create Seekerskillset objects.
So change:
seeker_skll = Skillset.objects.bulk_create(seeker_skll)
to:
seeker_skll = Seekerskillset.objects.bulk_create(seeker_skll)

Query inherited models

I have 3 Models:
Actor(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
type = models.Charfield(max_length=20)
Organisation(Actor):
name = models.Charfield(max_length=20)
actor = models.OneToOneField(Actor, on_delete=Models.CASCADE)
parent_organisation = models.ForeignKey(Actor, on_delete=models.CASCADE)
CustomUser(AbstractBaseUser,PermissionsMixin,Actor):
name = models.Charfield(max_length=20)
member_of = models.ManyToManyField(Actor, on_delete=models.CASCADE)
I am basically trying to get all users within an organisation:
User and Organisation share a common named id - Actor and Inherit from this class.
I have tried:
user_list = CustomerUser.objects.filter(parent__parent__id=request.session['organisation']
and
user_list = Organisation.objects.filter(parent__parent_user_organisation=request.session[''organisation']
and
user_list = Actor.objects.filter(parent__parent__user_organisation__in=request.session['organisation'])
although I would like to try and keep all queries on the actor model if possible. Eventually, id like to get all users within a multi-tiered organisation structure although for now, I'm happy to get just users based on Organisation_parent rather than ----- Organisation_parent_Organisation_parent etc...
I have changed my model structure to inherited to try and make queries easier but not having much luck.
The full trace:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-
packages/django/db/models/fields/__init__.py", line 2348, in to_python
return uuid.UUID(**{input_form: value})
File "/usr/local/lib/python3.8/uuid.py", line 171, in __init__
raise ValueError('badly formed hexadecimal UUID string')
During handling of the above exception (badly formed hexadecimal UUID string), another exception occurred: File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request) File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/src/app/administration/views.py", line 133, in user_list
user_list = Actor.objects.filter(parent__parent__user_organisation__in=request.session['organisation']) File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs)) File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases) File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter( File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value) File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs) File "/usr/local/lib/python3.8/site-packages/django/db/models/lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup() File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 59, in get_prep_lookup
self.rhs = [target_field.get_prep_value(v) for v in self.rhs] File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 59, in <listcomp>
self.rhs = [target_field.get_prep_value(v) for v in self.rhs] File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 945, in get_prep_value
return self.target_field.get_prep_value(value) File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2332, in get_prep_value
return self.to_python(value) File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2350, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /administration/administration/user/list Exception Value: ['“4” is not a valid UUID.']

combining __startswith and __in not working

I have a QuerySet and an array of strings that I want to test against the QuerySet.
The problem is the values I want to check are foreignKeys and the important characters of the foreignKey are in the beginning.
The query I thought would work is this:
materials_comparison_list.extend(materials_non_eu.filter(code__code__startswith__in=headings_list))
materials_non_eu is the QuerySet, headings_list is the array
However when running that it returns the following error:
django.core.exceptions.FieldError: Unsupported lookup 'startswith' for CharField or join on the field not permitted, perhaps you meant startswith or istartswith
I tried to change the place or __startswith and __in but that produces the same error (different words)
The models for materials looks like this:
class Materials(models.Model):
id = models.AutoField(primary_key=True)
row = models.IntegerField(null=True)
code = models.ForeignKey('HS_code', on_delete=models.CASCADE, null=True)
...
The model for the code looks like this:
class HS_Code(models.Model):
id = models.AutoField(primary_key=True)
code = models.CharField(max_length=10, unique=False)
....
The complete console output:
Traceback (most recent call last):
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
return func(*args, **kwargs)
File "/Users/5knnbdwm/Python_env/FlexOrigin/main/user.py", line 1115, in api_user_summary_v2
print(Calculation_Master(
File "/Users/5knnbdwm/Python_env/FlexOrigin/main/cluster_v2.py", line 60, in Calculation_Master
MAXNOM(session, materials, country, rule_block[1])
File "/Users/5knnbdwm/Python_env/FlexOrigin/main/cluster_v2.py", line 140, in MAXNOM
materials_comparison_list.extend(materials_non_eu.filter(
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1150, in build_lookup
lhs = self.try_transform(lhs, name)
File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1198, in try_transform
raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'startswith' for CharField or join on the field not permitted, perhaps you meant startswith or istartswith?
You can not combine the two, but you can make a disjunctive filter with a Q object:
from django.db.models import Q
materials_comparison_list.extend(
materials_non_eu.filter(Q(
*[('code__code__startswith', heading) for heading in headings_list],
_connector=Q.OR
))
)

Django get_or_create ValueError

Trying to add rows from a DataFrame into Django model.
models.py:
class CreditIndex_TEST(models.Model):
loanBook = models.ForeignKey(LoanBooks, on_delete=models.CASCADE)
run_date = models.DateField()
index_date = models.DateField()
index_CD = models.IntegerField()
Index_value = models.FloatField()
class Meta:
constraints = [
models.UniqueConstraint(fields= ['loanBook','run_date','index_date','index_CD'], name='unique_CreditIndexPair')
]
views.py:
for index, row in tempDf.iterrows():
obj, created = CreditIndex_TEST.objects.get_or_create(
loanBook=dbname,
run_date= run_date,
index_date=row['Date'],
index_CD=1,
Index_value=row['CreditIndex_CD1']
)
ERROR::
ValueError: Field 'id' expected a number but got 'Botswana_TU'.
I don't understand why I am being asked to specify id. The record I am trying to add to the model does not exist and therefore Django should create it and assign id, not expect it from me?
EDIT (full traceback):
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/gunthermarais/credit-risk/django/RiskLab/Provisions/views.py", line 48, in macroGUI
Index_value=row['CreditIndex_CD1']
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 575, in update_or_create
obj = self.select_for_update().get(**kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 404, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1381, in _add_q
check_filterable=check_filterable,
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
) from e
ValueError: Field 'id' expected a number but got 'Botswana_TU'.