**My Index.yaml file **
`
indexes:
- kind: company
properties:
- name: company_email
direction: asc # or DESC based on your query requirements
- name: company_name
direction: asc # or DESC based on your query requirements
`
**My Function: **
def get_company_name_by_email(company_email):
client = datastore.Client()
query = client.query(kind='company')
query.projection = ['company_name']
query.add_filter('company_email', '=', company_email)
query.keys_only()
query.limit = 1
results = list(query.fetch())
if results:
company_name = results[0]['company_name']
return company_name
else:
return None
** My Usage of this function in views.py **
** class UserLoginView(APIView):
def post(self, request):
serializer = UserLoginSerializer(data=request.data)
if serializer.is_valid():
# Update to match the serializer field names: company_email
email = serializer.validated_data.get('company_email')
# Debugging: Print the email obtained from the serializer
print("Email received:", email)
if email:
# Fetch company name associated with the email from the datastore
company_name = get_company_name_by_email(email)
if company_name:
# Authentication successful, return the company name along with the response
return Response({'message': 'Login successful', 'company_name': company_name}, status=status.HTTP_200_OK)
else:
# Handle scenario where company name is not found
return Response({'message': 'Company name not found for the given email'}, status=status.HTTP_404_NOT_FOUND)
else:
# Handle scenario where email is not found in the request data
return Response({'message': 'Email not provided or invalid'}, status=status.HTTP_400_BAD_REQUEST)
else:
# Handle serializer validation errors
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
**
**Error: **
Email received: /Users/emblemdevice/Desktop/final/Djangobackend/loginapp/datastore_ops.py:9: UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead. query.add_filter('company_email', '=', company_email) Internal Server Error: /api/login/ Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 75, in error_remapped_callable return callable_(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/grpc/_channel.py", line 1161, in call return _end_unary_response_blocking(state, call, False, None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/grpc/_channel.py", line 1004, in _end_unary_response_blocking raise _InactiveRpcError(state) # pytype: disable=not-instantiable ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.FAILED_PRECONDITION details = "The query requires an ASC or DESC index for kind company and property company_email. Please use the gcloud CLI to remove the single-property index exemption for this property." debug_error_string = "UNKNOWN:Error received from peer ipv4:172.217.17.42:443 {grpc_message:"The query requires an ASC or DESC index for kind company and property company_email. Please use the gcloud CLI to remove the single-property index exemption for this property.", grpc_status:9, created_time:"2023-12-20T13:40:36.704933+00:00"}"
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper return view_func(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view return self.dispatch(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/emblemdevice/Desktop/final/Djangobackend/loginapp/views.py", line 18, in post company_name = get_company_name_by_email(email) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/emblemdevice/Desktop/final/Djangobackend/loginapp/datastore_ops.py", line 13, in get_company_name_by_email results = list(query.fetch()) ^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/page_iterator.py", line 208, in _items_iter for page in self._page_iter(increment=False): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/page_iterator.py", line 244, in _page_iter page = self._next_page() ^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/cloud/datastore/query.py", line 814, in _next_page response_pb = self.client._datastore_api.run_query( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/cloud/datastore_v1/services/datastore/client.py", line 637, in run_query response = rpc( ^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in call return wrapped_func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/retry.py", line 366, in retry_wrapped_func return retry_target( ^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/retry.py", line 204, in retry_target return target() ^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 77, in error_remapped_callable raise exceptions.from_grpc_error(exc) from exc google.api_core.exceptions.FailedPrecondition: 400 The query requires an ASC or DESC index for kind company and property company_email. Please use the gcloud CLI to remove the single-property index exemption for this property. [20/Dec/2023 13:40:36] "POST /api/login/? HTTP/1.1" 500 155546