将参数注入原始查询python

时间:2019-01-24 01:26:50

标签: python django

使用(其中user_id = 1)时,我可以获得结果,但是当注入参数时,我得到了这样的回溯:'User'对象是不可迭代的,有关将参数注入原始查询的任何想法,

我的views.py:

 class TransactionViews(viewsets.ViewSet):
            def list(self, request):
                user = get_object_or_404(User, pk=request.user.id)
                queryset = Transaction.objects.raw("SELECT product.name, transaction.* from product inner join variant on product.id = variant.product_id inner join transactions_variants on variant.id = transactions_variants.variant_id inner join transaction on transactions_variants.transaction_id = transaction.id where user_id=%s",user)
                serializer = TransactionSerializer(queryset, many=True)
                return Response(serializer.data, status=200)

另一个问题:

queryset = Transaction.objects.filter(user_id=user).exclude(deleted_at__isnull=False)

我如何将这个“ .exclude(deleted_at__isnull = False)”转换为原始查询(这是软删除的条件)

Traceback:

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.7/site-packages/rest_framework/viewsets.py" in view
  116.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  495.             response = self.handle_exception(exc)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in handle_exception
  455.             self.raise_uncaught_exception(exc)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  492.             response = handler(request, *args, **kwargs)

File "/code/yashoes/transaction/views.py" in list
  29.         return Response(serializer.data, status=200)

File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py" in data
  765.         ret = super(ListSerializer, self).data

File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py" in data
  262.                 self._data = self.to_representation(self.instance)

File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py" in to_representation
  683.             self.child.to_representation(item) for item in iterable

File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in __iter__
  1339.         self._fetch_all()

File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in _fetch_all
  1326.             self._result_cache = list(self.iterator())

File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in iterator
  1349.         query = iter(self.query)

File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py" in __iter__
  96.         self._execute_query()

File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py" in _execute_query
  123.             params = tuple(adapter(val) for val in self.params)

Exception Type: TypeError at /api/user/transactions/
Exception Value: 'User' object is not iterable
Request information:
USER: phutran

2 个答案:

答案 0 :(得分:0)

As clearly and explicitely documented,即使只有一个参数,也需要将查询参数作为元组或列表传递。 IOW,您要:

queryset = Transaction.objects.raw("your long query here where user_id=%s", [user])

答案 1 :(得分:-1)

请尝试以下操作:

queryset = Transaction.objects.raw("SELECT product.name, transaction.* from product inner join variant on product.id = variant.product_id inner join transactions_variants on variant.id = transactions_variants.variant_id inner join transaction on transactions_variants.transaction_id = transaction.id where user_id=%s",user.id)