Opened 2 years ago

Closed 2 years ago

#33729 closed Bug (invalid)

AutocompleteSelect widget broken after moving from django 2.2 to django 3.2

Reported by: exo Owned by: nobody
Component: contrib.admin Version: 3.2
Severity: Normal Keywords: AutocompleteSelect
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by exo)

Hello :) After upgrading Django version to 3.2, widget AutocompleteSelect that I use in django admin panel (to have a drop-down from which I can choose an object) is broken.

The error I see is

    AttributeError at /admin/question/
    'QuerySet' object has no attribute 'name'

    Request Method:	GET
    Request URL:	http://localhost:8000/admin/question/
    Django Version:	3.2.13
    Exception Type:	AttributeError
    Exception Value:	
    'QuerySet' object has no attribute 'name'
    Exception Location:	/home/django-app/env/lib/python3.8/site-packages/django/contrib/admin/widgets.py, line 412, in build_attrs
    Python Executable:	/home/django-app/env/bin/python3
    Python Version:	3.8.10
    Python Path:	
    ['/home/django-app/testsite',
     '/usr/lib/python38.zip',
     '/usr/lib/python3.8',
     '/usr/lib/python3.8/lib-dynload',
     '/home/django-app/env/lib/python3.8/site-packages']
    Server time:	Fri, 20 May 2022 10:13:27 +0000
    Error during template rendering
    In template /home/django-app/testsite/polls/templates/admin/question_export.html, error at line 18

    'QuerySet' object has no attribute 'name'
    11	
    12	{% block content %}
    13	    <div id="content-main">
    14	      <p>Select question to export:</p>
    15	        <form method="post" enctype="multipart/form-data">
    16	            {% csrf_token %}
    17	            <table>
    18	                {{form.as_table}}
    19	            </table>
    20	            <div class="submit-row">
    21	                <input type="submit" value="Export Question" />
    22	            </div>
    23	        </form>
    24	    </div>
    25	    {{form.media}}
    26	{% endblock %}
    27	

AutocompleteSelect inherits from AutocompleteMixin

When I compare AutocompleteMixin for django 3.2 and django 2.2
https://github.com/django/django/blob/3.2.13/django/contrib/admin/widgets.py#L410-L412
https://github.com/django/django/blob/2.2.7/django/contrib/admin/widgets.py#L411

I see that they added new attributes

    'data-app-label': self.field.model._meta.app_label,
    'data-model-name': self.field.model._meta.model_name,
    'data-field-name': self.field.name,

in django 3.2
but there is no name on self.field and probably that's why I get this error.

The code looks like this

view.py

    from django.forms import ModelChoiceField
    from django import forms
    from django.contrib import admin
    from django.contrib.admin.widgets import AutocompleteSelect
    from django.http import HttpResponse

    def generate_response(question):
        # some code
        return HttpResponse(content_type="text/csv")

    class QuestionChoiceField(ModelChoiceField):
        widget = AutocompleteSelect(Question.objects.all(), admin.site)
    
    
    class QuestionExportForm(forms.Form):
        question = QuestionChoiceField(queryset=Question.objects.all(), required=True)
    
        def clean_question(self):
            return self.cleaned_data["question"]
    
    
    class QuestionExportView(FormView):
        template_name = "admin/question_export.html"
        form_class = QuestionExportForm
        success_url = "/admin/"
    
        def form_valid(self, form):
            question = form.cleaned_data.get("question")
            return generate_response(question)

models.py

    from django.db import models


    class Question(models.Model):
        uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')

templates/admin/question_export.html

    {% block content %}
        <div id="content-main">
          <p>Select question to export:</p>
            <form method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <table>
                    {{form.as_table}}
                </table>
                <div class="submit-row">
                    <input type="submit" value="Export Question" />
                </div>
            </form>
        </div>
        {{form.media}}
    {% endblock %}

urls.py

from django.contrib.admin.views.decorators import staff_member_required
from django.urls import path

from polls.views import QuestionExportView

app_name = "question_exports"


urlpatterns = [
    path("admin/question/", staff_member_required(QuestionExportView.as_view()), name="question"),
    path('admin/', admin.site.urls),
]

How can I approach this issue? Any help would be appreciated :)!

Change History (7)

in reply to:  description comment:1 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

Replying to exo:

Hello :) After upgrading Django version to 3.2, widget AutocompleteSelect that I use in django admin panel (to have a drop-down from which I can choose an object) is broken.

The error I see is

    AttributeError at /admin/question/
    'QuerySet' object has no attribute 'name'

    Request Method:	GET
    Request URL:	http://localhost:8000/admin/question/
    Django Version:	3.2.13
    Exception Type:	AttributeError
    Exception Value:	
    'QuerySet' object has no attribute 'name'
    Exception Location:	/home/django-app/env/lib/python3.8/site-packages/django/contrib/admin/widgets.py, line 412, in build_attrs
    Python Executable:	/home/django-app/env/bin/python3
    Python Version:	3.8.10
    Python Path:	
    ['/home/django-app/testsite',
     '/usr/lib/python38.zip',
     '/usr/lib/python3.8',
     '/usr/lib/python3.8/lib-dynload',
     '/home/django-app/env/lib/python3.8/site-packages']
    Server time:	Fri, 20 May 2022 10:13:27 +0000
    Error during template rendering
    In template /home/django-app/testsite/polls/templates/admin/question_export.html, error at line 18

    'QuerySet' object has no attribute 'name'
    11	
    12	{% block content %}
    13	    <div id="content-main">
    14	      <p>Select question to export:</p>
    15	        <form method="post" enctype="multipart/form-data">
    16	            {% csrf_token %}
    17	            <table>
    18	                {{form.as_table}}
    19	            </table>
    20	            <div class="submit-row">
    21	                <input type="submit" value="Export Question" />
    22	            </div>
    23	        </form>
    24	    </div>
    25	    {{form.media}}
    26	{% endblock %}
    27	

AutocompleteSelect inherits from AutocompleteMixin

When I compare AutocompleteMixin for django 3.2 and django 2.2
https://github.com/django/django/blob/3.2.13/django/contrib/admin/widgets.py#L410-L412
https://github.com/django/django/blob/2.2.7/django/contrib/admin/widgets.py#L411

I see that they added new attributes

    'data-app-label': self.field.model._meta.app_label,
    'data-model-name': self.field.model._meta.model_name,
    'data-field-name': self.field.name,

in django 3.2
but there is no name on self.field and probably that's why I get this error.

self.field should be a Field instance not a QuerySet. Moreover autocomplete works for me. It looks like an issue in 3rd-party package (see similar reports for autocomplete e.g. #32619 and #32659).

Closing per TicketClosingReasons/UseSupportChannels.

comment:2 by Mariusz Felisiak, 2 years ago

Summary: [BUG] AutocompleteSelect widget broken after moving from django 2.2 to django 3.2\AutocompleteSelect widget broken after moving from django 2.2 to django 3.2

comment:3 by Mariusz Felisiak, 2 years ago

Summary: \AutocompleteSelect widget broken after moving from django 2.2 to django 3.2AutocompleteSelect widget broken after moving from django 2.2 to django 3.2

comment:4 by exo, 2 years ago

Description: modified (diff)

comment:5 by exo, 2 years ago

Description: modified (diff)

comment:6 by exo, 2 years ago

Resolution: invalid
Status: closednew

Hi Mariusz Felisiak, thank you for taking a look!
My goal was to have a simple page with a form that contain dropdown to choose question and button to export chosen question data as csv file.

self.field should be a Field instance not a QuerySet.

Not sure if I understand what should be exactly changed in my example to make it working. Would you mind be more specific?

Moreover autocomplete works for me. It looks like an issue in 3rd-party package

You can run my example code for django 2.2.7 where AutocompleteSelect works fine and then install django 3.2.13 where it's broken. There is no 3rd-party package that is used here.
Thank you in advance! :)

comment:7 by Tim Graham, 2 years ago

Resolution: invalid
Status: newclosed

See TicketClosingReasons/UseSupportChannels for ways to get help. This ticket tracker is used only for confirmed bugs in Django.

Note: See TracTickets for help on using tickets.
Back to Top