vintasoftware / django-ai-assistant

Integrate AI Assistants with Django to build intelligent applications
https://vintasoftware.github.io/django-ai-assistant/
MIT License
204 stars 12 forks source link

Add auth required to Django Ninja API #57

Closed fjsj closed 2 months ago

fjsj commented 2 months ago

See https://django-ninja.dev/guides/authentication/

Otherwise this happens;

Field 'id' expected a number but got <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x78f3f4c6f800>>.
Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 2117, in get_prep_value
    return int(value)
           ^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'SimpleLazyObject'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 107, in run
    result = self.view_func(request, **values)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/workspace/django-ai-assistant/django_ai_assistant/views.py", line 47, in list_threads
    return list(get_threads(user=request.user, request=request, view=None))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/workspace/django-ai-assistant/django_ai_assistant/helpers/assistants.py", line 234, in get_threads
    return list(Thread.objects.filter(created_by=user))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1476, in filter
    return self._filter_or_exclude(False, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1494, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1501, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1613, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1645, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1559, in build_filter
    condition = self.build_lookup(lookups, col, value)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1389, in build_lookup
    lookup = lookup_class(lhs, rhs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/lookups.py", line 30, in __init__
    self.rhs = self.get_prep_lookup()
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/related_lookups.py", line 156, in get_prep_lookup
    self.rhs = target_field.get_prep_value(self.rhs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 2119, in get_prep_value
    raise e.__class__(
TypeError: Field 'id' expected a number but got <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x78f3f4c6f800>>.
ERROR    [2024-06-10 02:30:43,190] django: Field 'id' expected a number but got <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x78f3f4c6f800>>.
Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 2117, in get_prep_value
    return int(value)
           ^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'SimpleLazyObject'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/ninja/operation.py", line 107, in run
    result = self.view_func(request, **values)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/workspace/django-ai-assistant/django_ai_assistant/views.py", line 47, in list_threads
    return list(get_threads(user=request.user, request=request, view=None))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/workspace/django-ai-assistant/django_ai_assistant/helpers/assistants.py", line 234, in get_threads
    return list(Thread.objects.filter(created_by=user))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1476, in filter
    return self._filter_or_exclude(False, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1494, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1501, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1613, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1645, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1559, in build_filter
    condition = self.build_lookup(lookups, col, value)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/sql/query.py", line 1389, in build_lookup
    lookup = lookup_class(lhs, rhs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/lookups.py", line 30, in __init__
    self.rhs = self.get_prep_lookup()
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/related_lookups.py", line 156, in get_prep_lookup
    self.rhs = target_field.get_prep_value(self.rhs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/foobar/.cache/pypoetry/virtualenvs/django-ai-assistant-vsXVb2Qp-py3.12/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 2119, in get_prep_value
    raise e.__class__(
TypeError: Field 'id' expected a number but got <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x78f3f4c6f800>>.
Internal Server Error: /ai-assistant/threads/
ERROR    [2024-06-10 02:30:43,195] django.request: Internal Server Error: /ai-assistant/threads/
[10/Jun/2024 02:30:43] "GET /ai-assistant/threads/ HTTP/1.1" 500 4162
pamella commented 2 months ago

@fjsj Should I add csrf=True to the ninja API as well? https://django-ninja.dev/reference/csrf/