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