Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying both 'fields' and 'form_class' is not permitted

I have the following form, which I want render it with Django crispy forms.

This is my views.py

class RehabilitationSessionCreate(CreateView):
    model = RehabilitationSession
    form_class = RehabilitationSessionForm()

    success_url = reverse_lazy('rehabilitationsessions:list')

    fields = ['patient','medical','therapist','status','date_session_begin','upper_extremity',
              'pain_extremity','affected_segment','movement','metrics','time_movement','games',
              'game_levels','iterations','errors_numbers_game','time_level_acomplished',
              'patient_behavior','observations','date_session_end','period',]

class RehabilitationSessionUpdate(UpdateView):
    model = RehabilitationSession
    success_url = reverse_lazy('rehabilitationsessions:list')
    fields = ['patient', 'medical', 'therapist', 'status', 'date_session_begin', 'upper_extremity',
              'pain_extremity', 'affected_segment', 'movement', 'metrics', 'time_movement', 'games',
              'game_levels', 'iterations', 'errors_numbers_game', 'time_level_acomplished',
              'patient_behavior', 'observations', 'date_session_end', 'period', ]

This is my urls.py project main file :

from django.conf.urls import url, include
urlpatterns = [

    url(r'^sesiones-de-rehabilitacion/', include('medical_encounter_information.urls', namespace='rehabilitationsessions')),
    # Call the medical_encounter_information/urls.py

]

This is my medical_encounter_information/urls.py

from django.conf.urls import include, url, patterns
from .views import RehabilitationSessionCreate

urlpatterns = [

    url(r'^$', RehabilitationSessionList.as_view(), name='list'),

    url(r'^(?P<pk>\d+)$', RehabilitationSessionDetail.as_view(), name='detail'),

    url(r'^nuevo/', RehabilitationSessionCreate.as_view(), name='new'),

    url(r'^editar/(?P<pk>\d+)$', RehabilitationSessionUpdate.as_view(), name='edit'),

    url(r'^borrar/(?P<pk>\d+)$', RehabilitationSessionDelete.as_view(), name='delete'),

]

In my forms.py file I have:

class RehabilitationSessionForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        # user = kwargs.pop('user', None)
        super(RehabilitationSessionForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.add_input(Submit('submit', u'Save'))

    def save(self, commit=True):
        rehabilitation_session = super(RehabilitationSessionForm, self).save(commit=False)
        patient = self.cleaned_data['patient']

        if commit:
            rehabilitation_session.save()

        return rehabilitation_session

    class Meta:
        model = RehabilitationSession

        widgets = {
            'pain_extremity':forms.RadioSelect,
            'upper_extremity':forms.RadioSelect
        }
        fields = '__all__'

The template medical_encounter_information/templates/medical_encounter_information/rehabilitationsession_form.html is:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}Crear Registro{% endblock %}
{% block content %}

<div>
    {% crispy form %}
    {% csrf_token %}

</div>
{% endblock %}

When I type in my browser the url http://localhost:8000/sesiones-de-rehabilitacion/nuevo/ I get the following:

 File "/home/bgarcial/.virtualenvs/nrhb_dev/lib/python3.5/site-packages/django/views/generic/edit.py", line 138, in get_form_class
    "Specifying both 'fields' and 'form_class' is not permitted."
django.core.exceptions.ImproperlyConfigured: Specifying both 'fields' and 'form_class' is not permitted.
[11/Jul/2016 15:44:29] "GET /sesiones-de-rehabilitacion/nuevo/ HTTP/1.1" 500 100235

But, When I type in my browser the url http://localhost:8000/sesiones-de-rehabilitacion/editar/1 I view the following:

enter image description here

Because the view sesiones-de-rehabilitacion/editar/1 (RehabilitationSessionUpdate) is renderized, and the view sesiones-de-rehabilitacion/nuevo/ (RehabilitationSessionCreate) is not renderized knowing that together take the same template?

like image 668
bgarcial Avatar asked Dec 25 '22 03:12

bgarcial


1 Answers

As the error says, you cannot set both form_class and fields for your view. You can either set form_class

class RehabilitationSessionCreate(CreateView):
    model = RehabilitationSession
    form_class = RehabilitationSessionForm  # Note you should *not* have ()
    success_url = reverse_lazy('rehabilitationsessions:list')

Or you can set fields:

class RehabilitationSessionCreate(CreateView):
    model = RehabilitationSession
    fields = ['patient', ...]
    success_url = reverse_lazy('rehabilitationsessions:list')

You shouldn't need to set fields and form_class at the same time, because you can set fields on the form itself:

class RehabilitationSessionForm(forms.ModelForm):
    class Meta:
        fields = ['patient', ... ]
like image 114
Alasdair Avatar answered Dec 28 '22 07:12

Alasdair