getting error while using queryset Q in django view - python

I am creating the dynamic queryset using Q
while i am printing
build_query[:-1]
i am getting output
Q(owner_id=1)|Q(assigned_to=1)
but when using this in to
consultants = Consultant.objects.filter(*build_query[:-1])
getting an error too many values to unpack
i tried it with ** and without * still its not working.
when i check the type of
print type(build_query[:-1])
i am getting string type. Is the a main cause?
Traceback
Internal Server Error: /api/consultants/my_consultants/
Traceback (most recent call last):
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 85, in view
return self.dispatch(request, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/views.py", line 451, in dispatch
response = self.handle_exception(exc)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/views.py", line 448, in dispatch
response = handler(request, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/ckiller/consultants/api.py", line 170, in my_consultants
consultants = Consultant.objects.filter(*build_query[:-1])
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
Q(owner_id=1)|Q(assigned_to=1)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1331, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
consultants = Consultant.objects.filter(*build_query[:-1])

Did u try:
Consultant.objects.filter(*[build_query[:-1],])
args should be a list

Related

How to log in in existing password account with google auth django?

I have problem with google auth. Steps to reproduce problem:
Register password account
Try to log in with google auth
Get 500 answer
Traceback:
Internal Server Error: /api/v1/auth/google/
Traceback (most recent call last):
File "/home/andrew/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/andrew/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/views.py", line 54, in dispatch
return super().dispatch(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/andrew/Documents/pets_backend/project/api_v1/views/social.py", line 77, in post
response = super().post(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/views.py", line 125, in post
self.serializer.is_valid(raise_exception=True)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/serializers.py", line 422, in run_validation
value = self.validate(value)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/registration/serializers.py", line 151, in validate
complete_social_login(request, login)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 151, in complete_social_login
return _complete_social_login(request, sociallogin)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 172, in _complete_social_login
ret = _process_signup(request, sociallogin)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 22, in _process_signup
url = reverse("socialaccount_signup")
File "/home/andrew/.local/lib/python3.10/site-packages/django/urls/base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'socialaccount_signup' not found. 'socialaccount_signup' is not a valid view function or pa
ttern name.
"POST /api/v1/auth/google/ HTTP/1.1" 500 184486
I added to configs
SOCIALACCOUNT_EMAIL_VERIFICATION = "none"
SOCIALACCOUNT_EMAIL_REQUIRED = False
SOCIALACCOUNT_QUERY_EMAIL = False
but not worked.

How pass parameter in this method?

Hello I'm newbie Python user.
I study now django queryset. but
query.pyi in _BaseQuerySet's method
def values(self, *fields: Union[str, Combinable], **expressions: Any) -> ValuesQuerySet[_T, Dict[str, Any]]: ...
I want to pass a parameter to that function, but the first code runs, but the second code does not.
here is error code
valuelist = ("col1", "col2")
comment = Comment.objects.annotate(col1=F("contents"), col2=F("comment_depth")).values(valuelist)
and showed up if above code run
Traceback (most recent call last):
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\rest_framework\viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "D:\djangostudy\DjangoStudy\member\views.py", line 79, in memberlistrlist nt_depth")).values(valuelist)
comment = Comment.objects.annotate(col1=F("contents"), col2=F("commes\query.py", line 841, in valuesment_depth")).values(valuelist)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\db\models\query.py", line 836, in _valuesels\query.py", line 841, in values
clone = self._values(*fields, **expressions)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\db\models\query.py", line 836, in _values
clone.query.set_values(fields)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\db\models\sql\query.py", line 2172, in set_values
self.add_fields(field_names, True)
File "D:\Ryan\conda\envs\djangostudy\lib\site-packages\django\db\models\sql\query.py", line 1863, in add_fields
join_info = self.setup_joins(name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m)
ttributeError: 'tuple' object has no attribute 'split'
14/Mar/2021 19:14:09] "GET /member/memberlist/ HTTP/1.1" 500 116044
not error code
comment = Comment.objects.annotate(col1=F("contents"), col2=F("comment_depth")).values("col1", "col2")
What should I do to get it running normally?
The error is in the .values(…) clause: you can not pass a tuple as parameter. You use iterable unpack to unpack the values of an iterable as individual parameters. This is done by putting an asterisk (*) in front:
Comment.objects.annotate(
col1=F('contents'),
col2=F('comment_depth')
).values(*valuelist)
That being said, often it is not a good idea to work with .values(…) [Django-doc] since it "erodes" the model layer.

djongo.sql2mongo.SQLDecodeError: FAILED SQL: Error in django while filtering Data

I am trying to filter my product list with price less than filter but getting error. I guess the error is I am unable to pass parameters correctly.
any help would be appreciated.
if you require anymore details, please ask.
Note: it does not works even if I pass the query on filter body static.
thanks in advance.
my method:
def getfilter(request, format=None):
kwargs = {
'{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
# '{0}__{1}'.format('name', 'endswith'): 'Z'
}
products = Product.objects.all().filter(**kwargs)
serializer = ProductSerializer1(products, many=True)
return Response(serializer.data)
Error I Receive:
Traceback (most recent call last):
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 806, in parse
return handler(self, statement)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 934, in _select
self._query = SelectQuery(self.db, self.connection_properties, sm, self._params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 113, in __init__
super().__init__(*args)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 74, in __init__
self.parse()
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 153, in parse
raise SQLDecodeError
djongo.sql2mongo.SQLDecodeError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/decorators.py", line 55, in handler
return func(*args, **kwargs)
File "/home/sunil/Projects/python/loginBazaar/products/prodView.py", line 219, in getfilter
return Response(serializer.data)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 768, in data
ret = super(ListSerializer, self).data
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/cursor.py", line 53, in execute
params)
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 743, in __init__
self.parse()
File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 826, in parse
raise exe from e
djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT "products_product"."id", "products_product"."name", "products_product"."vendorId", "products_product"."quantity", "products_product"."shopCode", "products_product"."discountPercent", "products_product"."discountPrice", "products_product"."price", "products_product"."dummyPrice", "products_product"."margin", "products_product"."description", "products_product"."is_verified", "products_product"."category", "products_product"."subCategory", "products_product"."images", "products_product"."sizeChart", "products_product"."attributes", "products_product"."buyCount", "products_product"."thumbnail", "products_product"."productCode", "products_product"."atts" FROM "products_product" WHERE "products_product"."price" < %(0)s ORDER BY "products_product"."productCode" ASC
Version: 1.2.32
It seems like the conversion from Decimal to string failed:
'{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
and corresponding SQL:
< %(0)s
I guess one of this possible troubles:
your model field price type is incorrect for this conversion
your decimal value have two zero numbers after decimal point - try to
remove the last one

(django) error while overriding save() - name 'self' is not defined

I'm trying to override the method save() of my model:
class Article(models.Model):
number = models.PositiveIntegerField(editable=False)
def save(self, *args, **kwargs):
if not self.pk: #means: if objects wasn't saved before
self.number = Article.objects.count() +1 #number is 1-based
super(Article, self).save(*args, **kwargs)
Now I tried to add a new article from the admin page. The error I get:
self.number = Article.objects.count() +1
NameError: name 'self' is not defined
What am I doing wrong?
Some optional information:
number is some kind of ID, to order the objects in my way but these IDs shouldn't be changed manually
number is not allowed to be empty or blank, so I have to set a value before saving the first time (that's what I'm trying with that code snippet)
When an object is saved, the default number should be equal to the number of all objects (after saving), in 1-based counting
to change the order of the objects admin actions will be used
The whole traceback
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 544, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/sites.py", line 211, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1509, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1449, in changeform_view
self.save_model(request, new_object, form, not add)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1007, in save_model
obj.save()
File "/media/[directory to my projects]/projects_actual/myproject/myapp/models.py", line 69, in save
seĺf.number = Article.objects.count() + 1
NameError: name 'seĺf' is not defined
[16/Apr/2017 11:07:00] "POST /admin/myapp/article/add/ HTTP/1.1" 500 121043
Do you miss some information?

Django IntegrityError: Column 'position' cannot be null

I'm using Django-CMS and I created some custom plugins that I use in several pages but in one of the pages I can't publish nor I can't copy plugins that are nested and gives me the same error in both situations:
Internal Server Error: /en/admin/cms/page/copy-plugins/
Traceback (most recent call last):
File "lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "lib\site-packages\django\contrib\admin\sites.py", line 244, in inner
return view(request, *args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "lib\site-packages\django\views\decorators\http.py", line 42, in inner
return func(request, *args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "lib\site-packages\django\views\decorators\clickjacking.py", line 39, in wrapped_view
resp = view_func(*args, **kwargs)
File "lib\site-packages\django\utils\decorators.py", line 184, in inner
return func(*args, **kwargs)
File "lib\site-packages\cms\admin\placeholderadmin.py", line 363, in copy_plugins
plugins, target_placeholder, target_language, target_plugin_id)
File "lib\site-packages\cms\utils\copy_plugins.py", line 24, in copy_plugins_to
old_parent_cache, no_signals))
File "lib\site-packages\cms\models\pluginmodel.py", line 319, in copy_plugin
new_plugin.save()
File "lib\site-packages\cms\models\pluginmodel.py", line 240, in save
self.parent.add_child(instance=self)
File "lib\site-packages\treebeard\mp_tree.py", line 970, in add_child
return MP_AddChildHandler(self, **kwargs).process()
File "lib\site-packages\treebeard\mp_tree.py", line 361, in process
newobj.save()
File "lib\site-packages\cms\models\pluginmodel.py", line 248, in save
super(CMSPlugin, self).save(*args, **kwargs)
File "lib\site-packages\django\db\models\base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "lib\site-packages\django\db\models\base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "lib\site-packages\django\db\models\base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "lib\site-packages\django\db\models\base.py", line 859, in _do_insert
using=using, raw=raw)
File "lib\site-packages\django\db\models\manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "lib\site-packages\django\db\models\query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "lib\site-packages\django\db\models\sql\compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "lib\site-packages\django\db\backends\mysql\base.py", line 117, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "build\bdist.win-amd64\egg\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "build\bdist.win-amd64\egg\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1048, "Column 'position' cannot be null")
I've checked in my database and in the cms_cmsplugintable exists a column named position but there's no entry with position null. Do you have any idea what's going on?

Categories