Im trying to build a FormView for an app that needs to be subclassed afterwards. Sadly I was not able to set the formclass by the subclass.
My Code:
class EventCreateView(FormView):
template_name='Events/add_event_form.html'
success_url = reverse_lazy('events_list')
form_class = None # replaced by __init__ function
def __init__(self, *args, **kwargs):
self.form_class=EventForm
return super(EventCreateView, self).__init__(*args, **kwargs)
#other functions, not shown here ..
class TrainingCreateView(EventCreateView):
def __init__(self, *args, **kwargs):
self.form_class=TrainingForm
return super(TrainingCreateView, self).__init__(*args, **kwargs)
urls.py:
urlpatterns = patterns('',
url(r'event/event/add/$', EventCreateView.as_view(), name='event_add'),
url(r'event/training/add/$', TrainingCreateView.as_view(), name='training_add'),
)
What am I doing wrong?
Try this instead:
class EventCreateView(FormView):
template_name='Events/add_event_form.html'
success_url = reverse_lazy('events_list')
form_class = EventForm
...
class TrainingCreateView(EventCreateView):
form_class = TrainingForm
This doesn't work for the TrainingCreateView because the __init__ view does the following
It sets self.form_class = TrainingForm
super(TrainingCreateView, self).__init__(*args, **kwargs) calls the __init__ of EventCreateView ...
Which sets self.formclass = EventForm
You can get around this by changing the order of your __init_ method. Note that the method doesn't have to return anything.
class TrainingCreateView(EventCreateView):
def __init__(self, *args, **kwargs):
super(TrainingCreateView, self).__init__(*args, **kwargs)
self.form_class = TrainingForm
However, from the code you've written, it is not clear why you need to set self.form_class in the __init__ method, rather than just setting it as a class attribute. If you need to set it dynamically, a better option might be to override get_form_class instead.
Related
I want to filter form fields querysets based on the user selected. Therefore, I want to pass user as argument to the form in order to filter fields querysets in the form's __init__ method. When I pass any arguments to the form I get the following error.
class UserDetailView(LoginRequiredMixin, FormMixin, DetailView):
model = TbUser
form_class = TbPeopleEntranceRightForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
form = TbPeopleEntranceRightForm(user=self.object)
context['form'] = form
return context
__init__() got an unexpected keyword argument 'user'
how do I pass the argument correctly, and how I get it in the __init__ form method?
Update:
class TbPeopleEntranceRightForm(forms.ModelForm):
def __init__(self, user, **kwargs):
super().__init__(**kwargs)
print(user)
# Use `self.user` here or in some other methods.
__init__() missing 1 required positional argument: 'user'
don't do it in get_context_data, it's made for something else.
Use get_form_kwargs instead:
class UserDetailView(LoginRequiredMixin, FormMixin, DetailView):
model = TbUser
form_class = TbPeopleEntranceRightForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["user"] = self.object
return kwargs
And in your forms.py:
def __init__(self, user=None, *args, **kwargs):
self.user = user
super().__init__(*args, **kwargs)
You need to add the parameter to the form's __init__ method:
class TbPeopleEntranceRightForm(forms.ModelForm):
...
def __init__(self, user=None, **kwargs):
super().__init__(**kwargs)
self.user = user
# Use `self.user` here or in some other methods.
Also, the correct way to then pass the user argument to the form is to override get_form_kwargs in the view, like #MojixCoder showed.
I have this ModelForm:
class Event(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Event, self).__init__(*args, **kwargs)
##Here make some changes such as:
self.helper = FormHelper()
self.helper.form_method = 'POST'
##Many settings here which **i don't want to rewrite in 10 child classes**
class Meta:
model = Event
exclude = something...
widgets = some settings here also.
And this child ModelForm:
class UpgradedEvent(Event):
def __init__(self, *args, **kwargs):
super(UpgradedEvent,self).__init__(*args,**kwargs)
class Meta(Event.Meta):
model = UpgradedEvent
UpgradedEvent is a child of Event model but has some extra fields.
How can i inherit all the settings from the Event FORM into UpgradedEvent FORM?
When running the above code, it renders the Event form. Is there a way to inherit only the settings inside __init__ ?
EDIT
Check out the answer, it works great but keep in mind:
you need to create another instance of FormHelper in your child class, otherwise it won't work. So child class should look something like:
class UpgradedEvent(Event):
def __init__(self, *args, **kwargs):
super(UpgradedEvent,self).__init__(*args,**kwargs)
self.helper = FormHelper()
class Meta(Event.Meta):
model = UpgradedEvent
You can obtain the fields the Meta above, and extend the lists, etc.:
class UpgradedEventForm(EventForm):
def __init__(self, *args, **kwargs):
super(UpgradedEventForm,self).__init__(*args,**kwargs)
# some extra settings
# ...
# for example
self.fields['extra_field'].initial = 'initial value of extra field'
class Meta(EventForm.Meta):
model = UpgradedEvent
exclude = EventForm.Meta.exclude + ['extra_exclude1', 'extra_exclude2']
fields = EventForm.Meta.fields + ['extra_field']
So by using inheritance, we can add extra procedures to the __init__ function by performing some extra actions after the super(UpgradedEventForm, self) call, and wwe can access the attributes of our parent, and extend these.
Note that you better name your forms with a Form suffix, since now your models clash with your forms. As a result, your Form seems to have as model a reference to the Form itself. By using proper "nomenclature", you avoid a lot of mistakes.
Create FormWithSettings which will hold common settings for you form classes and inherit it
class FormWithSettings(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(FormWithSettings, self).__init__(*args, **kwargs)
##Here make some changes such as:
self.helper = FormHelper()
self.helper.form_method = 'POST'
##Many settings here which **i don't want to rewrite in 10 child classes**
class Meta:
exclude = something...
widgets = some settings here also.
class EventForm(FormWithSettings):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args,**kwargs)
class Meta(FormWithSettings.Meta):
model = Event
class UpgradedEventForm(FormWithSettings):
def __init__(self, *args, **kwargs):
super(UpgradedEventForm, self).__init__(*args,**kwargs)
class Meta(FormWithSettings.Meta):
model = UpgradedEvent
I'm using django:
I'm trying to pass a list of tuples from views.py to a dropdown box form but I get this attribute error
forms.py
import logging
from django import forms
log = logging.getLogger(__name__)
class TestForm(forms.Form):
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist',None)
log.info(regionlist)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)
super(TestForm, self).__init__(*args, **kwargs)
views.py
form = forms.RegionForm(regionlist=data)
Am I using the right method to pass variables between views.py and forms.py?
You need to call super first, so that the superclass sets up the fields attribute.
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist', None)
log.info(regionlist)
super(TestForm, self).__init__(*args, **kwargs)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)
I am trying to pass a variable to a ModelForm clean method using __init__ arguments but have had no success so far - I looked at various posts on StackOverflow but none seemed to help.
My code is the following:
forms.py
class property_booking_form(forms.ModelForm):
check_in_date = forms.DateField(widget=SelectDateWidget)
check_out_date = forms.DateField(widget=SelectDateWidget)
class Meta:
model = Properties_bookings
fields = ['check_in_date', 'check_out_date']
def __init__(self, property_id):
self.property_id = property_id
super(property_booking_form, self).__init__(self, property_id)
def clean(self):
check_in_date = self.cleaned_data.get('check_in_date')
check_out_date = self.cleaned_data.get('check_out_date')
property_min_nights = Properties.objects.get(id=self.property_id).property_minimum_nights
...
views.py
def view(request):
...
if request.method == 'POST':
booking_form = property_booking_form(request.POST, property_id=property_id)
if booking_form.is_valid():
...
else:
booking_form = property_booking_form(property_id=property_id)
return render(...)
This raises the following error:
'property_booking_form' object has no attribute 'get'
Which seems to be related to the widget as per the error description:
Exception Location:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/widgets.py in value_from_datadict, line 1058
The form works fine without the overriding __init__.
Does anyone know what would be the underlying cause of this issue?
Thanks.
Your __init__ method should accept *args and **kwargs, you should pass these when you call the superclass' __init__ method, rather than self and property_id.
def __init__(self, property_id, *args, **kwargs):
self.property_id = property_id
super(property_booking_form, self).__init__(*args, **kwargs)
You also need to change the way you instantiate the form in the view, since property_id is the first argument. For example:
if request.method == 'POST':
booking_form = property_booking_form(property_id=property_id, data=request.POST)
Alternatively, you can remove property_id from the signature, and pop it from kwargs. In this case, no changes to the views are required.
def __init__(self, *args, **kwargs):
self.property_id = kwargs.pop('property_id')
super(property_booking_form, self).__init__(*args, **kwargs)
This has been solved by amending __init__ as follows:
def __init__(self, *args, **kwargs):
self.property_id = kwargs.pop('property_id', None)
super(property_booking_form, self).__init__(*args, **kwargs)
I'm using the ModelFormSetView class in django-extra-views to create a formset view of all WorkerStatus entries connected to a Worker. I'd also like to use custom validation on the formset, so I've defined my own formset_class and form_class in the view. Here's the view definition:
class WorkerStatusUpdateView(ModelFormSetView):
model = WorkerStatusEntry
formset_class = WorkerStatusFormSet
form_class = WorkerStatusForm
template_name = 'staff/workers/worker_status_update.tmpl'
can_delete = True
can_order = False
fields = ['status', 'start_date']
def dispatch(self, request, *args, **kwargs):
self.worker = Worker.objects.get(pk=self.kwargs['worker_pk'])
return super(WorkerStatusUpdateView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, *args, **kwargs):
ctx = super(WorkerStatusUpdateView, self).get_context_data(*args, **kwargs)
ctx['worker'] = self.worker
return ctx
def get_queryset(self, *args, **kwargs):
return self.worker.statuses.all()
...and here are the definitions of the form and formset respectively:
class WorkerStatusForm(forms.ModelForm):
class Meta:
model = WorkerStatusEntry
fields = ['status', 'start_date']
class WorkerStatusFormSet(BaseModelFormSet):
class Meta:
model = WorkerStatusEntry
def __init__(self, queryset, *args, **kwargs):
super(WorkerStatusFormSet, self).__init__(*args, **kwargs)
def clean(self):
print "Cleaning"
This results in a page where EVERY WorkerStatusEntry in the database is shown in the formset, regardless of get_queryset(). One thing you'll notice is that WorkerStatusFormSet.__init__ takes a queryset argument: I put that there because there was a queryset argument passed to it from the ModelFormSetView, but I don't know what to do with it.
Another thing to note: if I take formset_class = WorkerStatusFormSet out of the view definition, the correct queryset shows up in the formset. However I need to use my own formset class to validate across the whole formset. Unless there's another way?
The problem is your WorkerStatusFormSet.__init__ method. Looking at the code for BaseModelFormSet, the __init__ method already takes a queryset parameter. Since you aren't doing anything in your __init__ method except calling super(), the easiest fix is to remove it.
It's not a good idea to change the signature of the __init__ method as you have done for two reasons
def __init__(self, queryset, *args, **kwargs):
super(WorkerStatusFormSet, self).__init__(*args, **kwargs)
You have changed the order of the arguments. If you look at the code for BaseModelFormset, the first argument is data. That means that data might be incorrectly assigned to queryset if somebody calls WorkerStatusFormSet(data, ...)
You do not do anything with queryset or pass it to super(), so it is lost.