django 1.6 - get related objects of related objects - django

I have the following three models:
class Region(models.Model):
code = models.IntegerField()
class ReportRequest(models.Model):
# a report can include many regions
regions = models.ManyToManyField(Region)
class Office(models.Model):
# an office belongs to one region
region = models.ForeignKey(Region)
basically, many reports can have many regions, and one region can have many offices.
How can I get all offices related to my report's regions in one query?
I can do this with two queries:
region_pks = reportrequest.regions.values_list('id', flat=True)
offices = Office.objects.filter(region_id__in=region_pks)
but I feel there must be a way to do this in just one query.
I know how to do this if this was a chain of objects connected by ForeignKeys, but ManyToManyField confuses me a bit.
update:
this way Office.objects.filter(region__in=reportrequest.regions) doesn't work.
reportrequest = ReportRequest.objects.first()
print(Office.objects.filter(region__in=reportrequest.regions).all())
Traceback (most recent call last):
File "/Users/9999/_projects/declarator/transparency/transparency/scripts/process_report_requests.py", line 22, in <module>
print(Office.objects.filter(region__in=reportrequest.regions).all())
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/modeltranslation/manager.py", line 338, in _filter_or_exclude
return super(MultilingualQuerySet, self)._filter_or_exclude(negate, *args, **kwargs)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q
current_negated=current_negated, connector=connector)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1142, in build_filter
value, lookups = self.prepare_lookup_value(value, lookups, can_reuse)
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1047, in prepare_lookup_value
value = value()
File "/Users/9999/.virtualenvs/transparency/lib/python2.7/site-packages/django/db/models/fields/related.py", line 839, in __call__
manager = getattr(self.model, kwargs.pop('manager'))
KeyError: u'manager'

This
offices = Office.objects.filter(region__in=reportrequest.regions.all())

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

I am trying to get data from database using python Django but got an error

Hello everyone I am trying to get data from database but it give me error in case when I am trying to get single data from db as shown below
wahid = Webapp.objects.get(title="Ecommerce Website")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 418, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1258, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1084, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Users\wahid\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1481, in names_to_path
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'title' into field. Choices are: created, demo_link, description, id, review, source_link, tags, tiltle, vote_ratio, vote_total
It will work only on getting all data from db as shown in image
There is a spelling mistake,
You have created field name "tiltle" in models, however you are trying to fetch "title" in your get query
Also, using get query might throw an error if there are more than one record that have title="Ecommerce Website"
for that you can use
Webapp.objects.filter(title="Ecommerce Website")
you should rename title field in your model, it seems you enter title field name to wrong dictation
'tiltle' must be 'title' in your model

In Django REST framework, how do I tell my serializer to update a member field instead of trying to create it from scratch?

I'm using Django 3 and Python 3.8 along with the Django rest framework. I have the following model. Notice the many-to-many relationship with addresses ...
class Coop(models.Model):
objects = CoopManager()
name = models.CharField(max_length=250, null=False)
types = models.ManyToManyField(CoopType, blank=False)
addresses = models.ManyToManyField(Address)
enabled = models.BooleanField(default=True, null=False)
phone = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_phone')
email = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_email')
web_site = models.TextField()
I have created the following serializers. I'm having trouble with updating the model so I exluded the create methods for simplicity ...
class AddressTypeField(serializers.PrimaryKeyRelatedField):
queryset = Address.objects
def to_internal_value(self, data):
if type(data) == dict:
locality = data['locality']
state = None if not re.match(r"[0-9]+", str(locality['state'])) else State.objects.get(pk=locality['state'])
locality['state'] = state
locality, created = Locality.objects.get_or_create(**locality)
data['locality'] = locality
address = Address.objects.create(**data)
# Replace the dict with the ID of the newly obtained object
data = address.pk
return super().to_internal_value(data)
...
class CoopSerializer(serializers.ModelSerializer):
types = CoopTypeSerializer(many=True, allow_empty=False)
addresses = AddressTypeField(many=True)
phone = ContactMethodPhoneSerializer()
email = ContactMethodEmailSerializer()
class Meta:
model = Coop
fields = '__all__'
...
def update(self, instance, validated_data):
"""
Update and return an existing `Coop` instance, given the validated data.
"""
instance.name = validated_data.get('name', instance.name)
try:
coop_types = validated_data['types']
instance.types.clear() # Disassociates all CoopTypes from instance.
for item in coop_types:
coop_type, _ = CoopType.objects.get_or_create(**item)
instance.types.add(coop_type)
except KeyError:
pass
instance.addresses = validated_data.get('addresses', instance.addresses)
instance.enabled = validated_data.get('enabled', instance.enabled)
phone = validated_data.pop('phone', {})
email = validated_data.pop('email', {})
instance.phone = ContactMethod.objects.create(type=ContactMethod.ContactTypes.PHONE, **phone)
instance.email = ContactMethod.objects.create(type=ContactMethod.ContactTypes.EMAIL, **email)
instance.web_site = validated_data.get('web_site', instance.web_site)
instance.web_site = validated_data.get('web_site', instance.web_site)
instance.save()
return instance
What's the right way to handle updating the address fields of my main Coop model? When I submit a payload like below (I have IDs included with my address member field objects) ...
{"id":927,"types":[{"id":76,"name":"ct"}],"addresses":[{"id":824,"street_number":"","route":"","raw":"","formatted":"4750 N Woodlawn Rd","latitude":null,"longitude":null,"locality":{"id":54,"name":"Chicago","postal_code":"60640","state":19313}}],"phone":{"phone":"3039468888"},"email":{"email":"dave#hello.com"},"name":"Dave Coop","enabled":true,"web_site":"http://www.example.org"}
And submit it via PATCH, I get the error below. Is there some way to tell my serializer to only update the member field if an ID is created instead of trying to create it from scratch each time?
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/views.py", line 78, in put
if serializer.is_valid():
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 234, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 433, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 490, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/fields.py", line 565, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 519, in to_internal_value
return [
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 520, in <listcomp>
self.child_relation.to_internal_value(item)
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 35, in to_internal_value
address = Address.objects.create(**data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
updated = self._save_table(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 886, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 923, in _do_insert
return manager._insert(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1377, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.IntegrityError: (1062, "Duplicate entry '824' for key 'address_address.PRIMARY'")
try using partial=True in serializer

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 error while searching in unaccent way

models.py
class KeySkills(models.Model):
skills = models.TextField()
versions = models.DecimalField(decimal_places=3,null=True,blank=True,max_digits=10,default=None)
experience = models.DecimalField(decimal_places=3,null=True,blank=True,max_digits=10,default=None)
user = models.ForeignKey(access_models.SeekerRegister,on_delete=models.CASCADE,related_name='key_skills',null=True,blank=True)
def __str__(self):
return "KeySkills"
query:
KeySkills.objects.filter(skills__unaccent__icontains='python')
KeySkills.objects.filter(skills_text__search='python')
error:
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.6/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.6/site-packages/django/db/models/query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
split_subq=split_subq,
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1225, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1080, in build_lookup
lhs = self.try_transform(lhs, name)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1126, in try_transform
(name, lhs.output_field.__class__.__name__))
django.core.exceptions.FieldError: Unsupported lookup 'unaccent' for TextField or join on the field not permitted.
Here i am trying to search using django ORM queries.
And tring using above two way of queries but i am getting above error .
Please have a look into my code.
You need to add 'django.contrib.postgres' in your INSTALLED_APPS
Also, try this link
You need to add 'django.contrib.postgres' in your INSTALLED_APPS:
and after that you need install the extension 'unaccent' to you database to do this execute tha query:
CREATE EXTENSION unaccent;
i did this inside pgadmin4 in execute query.