I have a SessionWizardView
that generate 3 forms, depending of of a previous choice. After the selection of the form, the user fill the form and submit it to a specific ListView
to each case, using POST. The problem are to get these values, mainly the Primary Key.
The SessionWizardView
, I followed the documentation (https://django-formtools.readthedocs.io/en/stable/wizard.html#wizard-template-for-each-form) and it's working as I expected. The 3 forms has the last field pre filled, depending from the first field.
In the ListView
, I've tried to use request.session.pop()
to get the fields from the SessionWizardView
. Here I had my first doubt: I used either the variable name in the forms.py
(e.g.:request.session.pop('formField1', {})
), and the field id in the POST (request.session.pop('opcao1-formField1', {})
). In both cases the browser return the 'int' object has no attribute 'pk'
error.
I've tested the 3 forms that the Wizard can show me and all of them show the same error, so I will put the code for the first one, so text stay short.
urls.py:
logger = logging.getLogger(__name__)
urlpatterns = [
# path('', views.IndexView.as_view(), name="index"),
path('', views.IndexView.as_view(views.FORMS), name="index"),
path('detail/', views.DetailView.as_view(), name="detail"),
views.py:
class IndexView(SessionWizardView):
condition_dict = {"opcao1" : graficoEscolhido1,
"opcao2" : graficoEscolhido2,
"opcao3" : graficoEscolhido3,
}
template_name = 'solid/index.html'
success_url = reverse_lazy('detail')
def done(self, form_list, **kwargs):
self.request.session['opcao1-dateDataInicial'] = str(form_list.cleaned_data['opcao1-dateDataInicial'])
self.request.session['opcao1-dateDataFinal'] = str(form_list.cleaned_data['opcao1-dateDataFinal'])
self.request.session['opcao1-codigoDuto'] = form_list.cleaned_data['opcao1-codigoDuto']
self.request.session['opcao1-codigoExtremidade'] = float(form_list.cleaned_data['opcao1-codigoExtremidade'])
# I've already commented the previous line, trade wizard for
# request. Put form_list.get_all_cleaned_data(), too as test.
return render(self.request, "solid/detail.html", {
'form_data': [form.cleaned_data for form in form_list],
})
class DetailView(generic.ListView):
def get_context_data(self, **kwargs):
cursor = connection.cursor()
# Trying to get from previous session. I've replaced request for
# wizard too, in previous attempt.
inicio = self.request.session.pop('opcao1-dateDataInicial', {})
final = self.request.session.pop('opcao1-dateDataFinal', {})
cod_id = self.request.session.pop('opcao1-codigoID', {})
extra = self.request.session.pop('opcao1-valorExtra', {})
Temp1 = TEMP.objects.values_list('TE_CD_ID_S_Temp', flat = True).filter(
TE_CD_ID_Cod = cod_id,
TE_VL_Ponto = extra)
Dens1 = DENS.objects.values_list('DE_CD_ID_S_Den', flat = True).filter(
DE_CD_ID_Cod = cod_id,
DE_VL_Ponto = extra)
Vaz1 = VAZ.objects.values_list('VA_CD_ID_S_Vaz', flat = True).filter(
VA_CD_ID_Cod = cod_id,
VA_VL_Ponto = extra)
Pres1 = PRES.objects.values_list('PR_CD_ID_S_Pres', flat = True).filter(
PR_CD_ID_Cod = cod_id,
PR_VL_Ponto = extra)
cursor.execute('giant query',(str(sensorPres[0]), inicio, final,
str(sensorTemp[0]), inicio, final,
str(sensorDens[0]), inicio, final,
str(sensorVaz[0]), inicio, final))
lista = cursor.fetchall()
listaArray = np.asarray(lista)
tempo = listaArray[:,0]
valpres = listaArray[:,1]
valpres = np.array(valpres)
valpres = valpres.astype(float)
valtemp = listaArray[:,2]
valtemp = np.array(valtemp)
valtemp = valtemp.astype(float)
valden = listaArray[:,3]
valden = np.array(valden)
valden = valden.astype(float)
valvaz = listaArray[:,4]
valvaz = np.array(valvaz)
valvaz = valvaz.astype(float)
kwargs['tempo'] = tempo
kwargs['valpres'] = valpres
kwargs['valtemp'] = valtemp
kwargs['valden'] = valden
kwargs['valvaz'] = valvaz
kwargs['dateInicial'] = inicio
kwargs['dateFinal'] = final
return super().get_context_data(**kwargs)
forms.py:
class get_Codigo_DutoGrafico1(forms.Form):
# codigoID is a PK in the model.
codigoID = forms.ModelMultipleChoiceField(label = "Código ID",
queryset = PRINCIPAL.objects.values_list('CD_ID', flat = True),
widget = forms.SelectMultiple(attrs = {'id' : 'id_codigo_id'}))
dateDataInicial = forms.DateTimeField(label = 'Data Inicial',
input_formats = ['%d/%m/%Y %H:%M:%S'],
widget=XDSoftDateTimePickerInput())
dateDataFinal = forms.DateTimeField(label = 'Data Final',
input_formats = ['%d/%m/%Y %H:%M:%S'],
widget=XDSoftDateTimePickerInput())
valorExtra = forms.ModelMultipleChoiceField(label = 'Valor Extra',
queryset = DENS.objects.values_list('DE_VL_Ponto', flat = True).none(),
widget = forms.SelectMultiple(attrs = {'id' : 'id_codigoDropDown'}))
index.html:
<div class = "container-fluid" id = "wrapper">
<div class = "row">
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action = "" method = "post" id = "formGrafico" data-data-url="{% url 'ajax_load_duto' %}" data-data2-url="{% url 'ajax_load_data2' %}" novalidate>
{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}
I expect to get the values in the detail.html and show the data in different ways. However, I get the following error: 'int' object has no attribute 'pk'. The POST is returning the following:
POST
Variable Value
csrfmiddlewaretoken
'TArU7O0laFVnRiWp9rEpUOE4bUoPNYulB2zNkFbwnLFxbVBwr4Qmp7ZQ5FfhJ3r7'
index_view-current_step
'opcao1'
opcao1-codigoDuto
'177'
opcao1-dateDataInicial
'03/05/2018 10:19:00'
opcao1-dateDataFinal
'01/07/2018 10:20:00'
opcao1-codigoExtremidade
'2,127000000000000'
The entire error traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 2.1.7
Python Version: 3.6.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'compressor',
'django_extensions',
'solid',
'sekizai',
'formtools']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/formtools/wizard/views.py" in dispatch
248. response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/formtools/wizard/views.py" in post
301. if form.is_valid():
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/forms.py" in is_valid
185. return self.is_bound and not self.errors
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/forms.py" in errors
180. self.full_clean()
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/forms.py" in full_clean
381. self._clean_fields()
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/forms.py" in _clean_fields
399. value = field.clean(value)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/models.py" in clean
1293. qs = self._check_values(value)
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/models.py" in _check_values
1326. pks = {str(getattr(o, key)) for o in qs}
File "/home/luiz/Projeto/Solid/venv/lib/python3.6/site-packages/django/forms/models.py" in <setcomp>
1326. pks = {str(getattr(o, key)) for o in qs}
Exception Type: AttributeError at /
Exception Value: 'int' object has no attribute 'pk'
So the data is passing from SessionWizardView, but something in the middle is wrong. To test the 'int' problem, I've set str() to the "cod_id" and still got 'int' error message. I've searched in the Stack something related to it, but I found nothing.
The error is because your ModelMultipleChoiceField
has a queryset
attribute set to an integer list, that's wrong, your queryset should be a list of objects: PRINCIPAL.objects.all()
. The ModelMultipleChoiceField
receives an integer as input (the posted data), but wants an object (PRINCIPAL
) as value
.
If you want to customise how the values are displayed to the user, you should subclass ModelMultipleChoiceField
and override the label_from_instance
method:
class IDModelMultipleChoiceField(ModelMultipleChoiceField):
def label_from_instance(self, instance):
return instance.pk
and in your form
cogidoID = IDModelMultipleChoiceField(queryset=..., label=...)