Search code examples
djangodjango-autocomplete-light

create new does returns 'list' object has no attribute 'create'


I am trying to create a autocomplete input which can create a new object.

class CellLineAutocomplete(autocomplete.Select2QuerySetView):
    create_field = 'cell_line_name'
    model = CellLine
    def has_add_permission(self, request):
        return True

    def get_queryset(self):
        if self.q:
            return CellLine.objects.filter(cell_line_name__icontains=self.q)
        return []

    def get_result_label(self, item):
        return item.cell_line_name

When clicking the create option I receive the following error:

Traceback (most recent call last):
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/dal/views.py", line 48, in dispatch
    return super(ViewMixin, self).dispatch(request, *args, **kwargs)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/dal/views.py", line 116, in post
    result = self.create_object(text)
  File "/Users/xy/.virtualenvs/z/lib/python3.6/site-packages/dal/views.py", line 92, in create_object
    return self.get_queryset().create(**{self.create_field: text})
AttributeError: 'list' object has no attribute 'create'

I access the code of the autocomplete git repo:

def create_object(self, text):
    """Create an object given a text."""
    return self.get_queryset().get_or_create(
        **{self.create_field: text})[0]

and edited it to work for me:

def create_object(self, text):
    """Create an object given a text."""
    return self.model.objects.get_or_create(
        **{self.create_field: text})[0]

Is there a better way to solve my problem than by hacking the code?

Thanks


Solution

  • You're returning a list from get_queryset in some cases, so the error should not be surprising.

    You should always return a queryset from that method; you can return an empty one with none():

    def get_queryset(self):
        if self.q:
            return CellLine.objects.filter(cell_line_name__icontains=self.q)
        return CellLine.objects.none()