Re: [Django] #33729: AutocompleteSelect widget broken after moving from django 2.2 to django 3.2

169 views
Skip to first unread message

Django

unread,
May 22, 2022, 11:16:35 AM5/22/22
to django-...@googlegroups.com
#33729: AutocompleteSelect widget broken after moving from django 2.2 to django 3.2
------------------------------------+--------------------------------------
Reporter: exo | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: AutocompleteSelect | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by exo:

Old description:

> 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
>
> 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):
> 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 %}
> }}}
>
> How can I approach this issue? Any help would be appreciated :)!

New description:

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

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 :)!

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33729#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 22, 2022, 11:47:44 AM5/22/22
to django-...@googlegroups.com

Old description:

> class Question(models.Model):

New description:

from django.http import HttpResponse

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

from polls.views import QuestionExportView

app_name = "question_exports"

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33729#comment:5>

Django

unread,
May 22, 2022, 12:06:19 PM5/22/22
to django-...@googlegroups.com
#33729: AutocompleteSelect widget broken after moving from django 2.2 to django 3.2
------------------------------------+--------------------------------------
Reporter: exo | Owner: nobody
Type: Bug | Status: new

Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:
Keywords: AutocompleteSelect | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by exo):

* status: closed => new
* resolution: invalid =>


Comment:

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! :)

--
Ticket URL: <https://code.djangoproject.com/ticket/33729#comment:6>

Django

unread,
May 22, 2022, 3:49:16 PM5/22/22
to django-...@googlegroups.com
#33729: AutocompleteSelect widget broken after moving from django 2.2 to django 3.2
------------------------------------+--------------------------------------
Reporter: exo | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: AutocompleteSelect | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/33729#comment:7>

Reply all
Reply to author
Forward
0 new messages