Say I have to override a ModelForm (in forms.py) to set labels or widgets differently. i.e:
class SomeForm(ModelForm):
def __init__(self,*args,**kwargs):
super(someForm, self).__init__(*args, **kwargs)
self.fields['someVal'].label = ...
self.fields['someVal'].widget = ...
How do I then set required=True?
I know I can do this before the __init__ like so:
class SomeForm(ModelForm):
someVal = CharField(required=True) # << here
def __init__(self,*args,**kwargs):
super(someForm, self).__init__(*args, **kwargs)
self.fields['someVal'].label = ...
self.fields['someVal'].widget = ...
But I'm doing some dynamic stuff and I'd like to set the required in the __init__
self.fields['someVal'].required = True
yes, it's just as simple as this :) you have the same attributes available to the Class in your fields dictionary
You need to override the fields in the form. Just use the same field names as in the model:
class SomeForm(forms.ModelForm):
field_a = forms.CharField(
'My new label', max_length=30, required=True) # << here
def __init__(self,*args,**kwargs):
super(someForm, self).__init__(*args, **kwargs)
class Meta:
model = MyModel
Why this approach?
I think overriding the fields directly by defining a new field is a good way if you want to override many attributes e.g. required, label, widget
Related
this is my code.
models.py
class Preventivo(models.Model):
prestazione1 = models.ForeignKey('Prestazione',on_delete=models.CASCADE, related_name="prestazione1")
form.py
class PreventivoForm(forms.ModelForm):
class Meta:
model = Preventivo
fields = ['cliente','prestazione1']
You can redefine the field to give it a custom queryset that's ordered how you want.
class PreventivoForm(forms.ModelForm):
prestazione1 = forms.ModelChoiceField(queryset=Prestazione.objects.order_by('name'))
class Meta:
model = Preventivo
fields = ['cliente','prestazione1']
You can change it in the __init__ method of your ModelForm.
This comes in handy if you need to pass other information to the Form, which you can then read from the kwargs.
class PreventivoForm(forms.ModelForm):
class Meta:
model = Preventivo
fields = ['cliente','prestazione1']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['prestazione1'].queryset = Preventivo.objects.order_by('<FIELDNAME>')
I'm trying to change the widget to fields in a form which have a string in the name, I'm trying to do something like the following:
class CI_tableForm(ModelForm):
class Meta:
model = CI_table
fields = report_query_values
for field in report_query_values:
if "_id" in field:
field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
Not sure if it's possible or not.
At the moment it doesn't error, but doesn't change the widget either.
Thanks,
Isaac
You should do it in the __init__ constructor:
class CI_tableForm(ModelForm):
class Meta:
model = CI_table
fields = report_query_values
def __init__(self, *args, **kwargs):
super(CI_tableForm, self).__init__(*args, **kwargs)
for field in report_query_values:
if "_id" in field:
choices = self.fields[field].widget.choices
self.fields[field].widget = forms.CheckboxSelectMultiple(
choices=choices)
All,
Is there a straightforward way for me to display a form that uses a modelchoicefield for models that have yet to be saved?
class Foo(models.Model):
name = models.CharField(max_length=42)
class Bar(models.Model):
name = models.CharField(max_length=42)
foo = models.ForeignKey("Foo",blank=True,null=True)
class BarForm(ModelForm):
class Meta:
model = Bar
pass
def create_new_bar(request):
foos = [Foo(name='foo1'), Foo(name='foo2'), Foo(name='foo3')]
bar = Bar(name='bar',foo=foos[0])
form = BarForm(instance=bar)
return render_to_response('bar_template.html',{"form" : form},context_instance=RequestContext(request))
But the "select" widget that gets rendered in the form has empty content. Presumably, this is because there is nothing to bind the queryset to for the ModelChoiceField that Bar's ForeignKey to Foo uses. (Since nothing in "foos" has been saved and therefore does not exist in the database).
Is there another way to deal with this besides writing a custom formfield/widget?
Thanks.
You can add additional field to BarForm with predefined choices - fooempty for example. That field will be shown in case if Foo table is empty. Try this (not tested):
class BarForm(ModelForm):
FOO_CHOICES = (
(0, 'Choice1'),
(1, 'Choice2'),
(2, 'Choice3'),
)
fooempty = forms.ChoiceField(required=False, label='lbl', choices=FOO_CHOICES)
class Meta:
model = Bar
def __init__(self, *args, **kwargs):
super(BarForm, self).__init__(*args, **kwargs)
if self.fields['foo'].queryset.count(): # Foo empty?
self.fields['fooempty'].widget = forms.HiddenInput()
else:
self.emptyflag
self.fields['foo'].widget = forms.HiddenInput()
def save(self, commit=True):
bar = super(BarForm, self).save(commit=False)
if self.emptyflag:
bar.foo_id = self.cleaned_data['fooempty']
bar.save()
return bar
EDIT: actually, now that I think about it. Those Foos won't have pks. So there is no way that could ever work. The form needs to know the pk of the Foos. You really just need to save them first. How would django know which one is which when it posts back the form? IGNORE ANSWER BELOW.
I think you can change the choices variable on the field. You will need to that in the form. So pass the list of Foos to the init of the form. Something like this:
class BarForm(ModelForm):
class Meta:
model = Bar
def __init__(self, foo_list, *args, **kwargs):
super(BarForm, self).__init__(*args, **kwargs)
self.fields['foo'].choices = foo_list
Then in the view:
foos = [Foo(name='foo1'), Foo(name='foo2'), Foo(name='foo3')]
bar = Bar(name='bar', foo=foos[0])
form = BarForm(foos, instance=bar)
I know I've done something like this before. I just can't remember the exact way. Something like this approach should work though.
taking my initial lessons with django ModelForm ,I wanted to give the user ,ability to edit an entry in a blog.The BlogEntry has a date,postedTime, title and content.I want to show the user an editform which shows all these fields,but with only title and content as editable. The date and postedTime should be shown as uneditable.
class BlogEntry(models.Model):
title = models.CharField(unique=True,max_length=50)
description = models.TextField(blank=True)
date = models.DateField(default=datetime.date.today)
postedTime = models.TimeField(null=True)
...
For adding an entry ,I use a ModelForm in the normal way..
class BlogEntryAddForm(ModelForm):
class Meta:
model = BlogEntry
...
But how do I create the edit form?I want it to show the date,postedTime as uneditable (but still show them on the form) and let the user edit the title and description.
if I use,exclude in class Meta for date and postedTime,that will cause them not to appear on the form.So,how can I show them as uneditable?
class BlogEntryEditForm(ModelForm):
class Meta:
model = BlogEntry
...?...
In the form object, declare the attribute of the field as readonly:
form.fields['field'].widget.attrs['readonly'] = True
Is date field represent a date when the entry first created or when it was modified last time? If first then use auto_now_add option else use auto_now. That is:
date = models.DateField(auto_now_add=True)
will set date to now when entry will be created.
auto_now_add makes field uneditable. For other cases use editable option to make any field uneditable. For example
postedDate = models.TimeField(null=True, editable=False)
Also, likely you will add posted boolean field to Entry model, so it is convinient to set auto_now on postedDate. It will set postedDate to now every time you modify a Entry including one when you set posted to True.
I implemented it this way: https://djangosnippets.org/snippets/10514/
this implementation uses the data of model instance for all read-only fields and not the data obtained while processing the form
below the same code but using his example
from __future__ import unicode_literals
from django.utils import six
from django.utils.encoding import force_str
__all__ = (
'ReadOnlyFieldsMixin',
'new_readonly_form_class'
)
class ReadOnlyFieldsMixin(object):
"""Usage:
class MyFormAllFieldsReadOnly(ReadOnlyFieldsMixin, forms.Form):
...
class MyFormSelectedFieldsReadOnly(ReadOnlyFieldsMixin, forms.Form):
readonly_fields = ('field1', 'field2')
...
"""
readonly_fields = ()
def __init__(self, *args, **kwargs):
super(ReadOnlyFieldsMixin, self).__init__(*args, **kwargs)
self.define_readonly_fields(self.fields)
def clean(self):
cleaned_data = super(ReadOnlyFieldsMixin, self).clean()
for field_name, field in six.iteritems(self.fields):
if self._must_be_readonly(field_name):
cleaned_data[field_name] = getattr(self.instance, field_name)
return cleaned_data
def define_readonly_fields(self, field_list):
fields = [field for field_name, field in six.iteritems(field_list)
if self._must_be_readonly(field_name)]
map(lambda field: self._set_readonly(field), fields)
def _all_fields(self):
return not bool(self.readonly_fields)
def _set_readonly(self, field):
field.widget.attrs['disabled'] = 'true'
field.required = False
def _must_be_readonly(self, field_name):
return field_name in self.readonly_fields or self._all_fields()
def new_readonly_form_class(form_class, readonly_fields=()):
name = force_str("ReadOnly{}".format(form_class.__name__))
class_fields = {'readonly_fields': readonly_fields}
return type(name, (ReadOnlyFieldsMixin, form_class), class_fields)
Usage:
class BlogEntry(models.Model):
title = models.CharField(unique=True,max_length=50)
description = models.TextField(blank=True)
date = models.DateField(default=datetime.date.today)
postedTime = models.TimeField(null=True)
# all fields are readonly
class BlogEntryReadOnlyForm(ReadOnlyFieldsMixin, forms.ModelForm):
class Meta:
model = BlogEntry
# selected fields are readonly
class BlogEntryReadOnlyForm2(ReadOnlyFieldsMixin, forms.ModelForm):
readonly_fields = ('date', 'postedTime')
class Meta:
model = BlogEntry
or use the function
class BlogEntryForm(forms.ModelForm):
class Meta:
model = BlogEntry
BlogEntryFormReadOnlyForm = new_readonly_form_class(BlogEntryForm, readonly_fields=('description', ))
This will prevent any user from hacking the request:
self.fields['is_admin'].disabled = True
Custom form example:
class MemberShipInlineForm(forms.ModelForm):
is_admin = forms.BooleanField(required=False)
def __init__(self, *args, **kwargs):
super(MemberShipInlineForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs and kwargs['instance'].is_group_creator:
self.fields['is_admin'].disabled = True
class Meta:
model = MemberShip
fields = '__all__'
From the documentation,
class BlogEntryEditForm(ModelForm):
class Meta:
model = BlogEntry
readonly_fields = ['date','postedTime']
I have a model and a form:
class MyModel(models.Model):
field_foo = models.CharField(_("Foo"), max_length=50)
field_bar = models.IntegerField(_("Bar"))
class MyFormOne(forms.ModelForm):
class Meta:
model=MyModel
fields = ('field_foo', )
widgets = {'field_foo': forms.TextInput(attrs={'size': 10, 'maxlength': 50}),}
I would like to have another form MyFormTwo which would subclass that form by including also the field field_bar. My point is not to have to repeat the widget declaration for field_foo in the second form (DRY principle), and also not to have to repeat the list of fields from MyFormOne (in reality there are much more then one field then in the simple example above).
How should I define MyFormTwo?
You shouldn't have to explicitly declare the entire widget, just modify the attrs which are different.
Or if you have custom widgets in the real code, I would either
Create a custom model field class as well which uses that widget by default, if it's a general condition, so in the form class it just works "automagically".
If it's just form specific (not model specific), then for that case I'd just declare the form field explicitly on Form class, not in Meta and then inheritance applies in a straightforward way.
But with default widgets (with custom attrs) I'd try something like the following
class MyModel(models.Model):
field_foo = models.CharField(_("Foo"), max_length=50)
field_bar = models.IntegerField(_("Bar"))
class MyFormOne(forms.ModelForm):
class Meta:
model=MyModel
fields = ('field_foo', )
def __init__(*args, **kwargs):
super(MyFormOne, self).__init__(*args, **kwargs)
self.fields['field_foo'].widget.attrs['size'] = 10
# max_length should already be set automatically from model
# TextInput is also default widget for CharField
class MyFormTwo(MyFormOne):
class Meta:
model=MyModel
fields = MyFormOne.Meta.fields + ('field_foo2',)
def __init__(*args, **kwargs):
super(MyFormTwo, self).__init__(*args, **kwargs)
self.fields['field_foo2'].widget.attrs['size'] = 10
I'm not sure if this will work or not (it's completely untested with Django forms/models), but if it does let me know.
class MyModel(models.Model):
field_foo = models.CharField(_("Foo"), max_length=50)
field_bar = models.IntegerField(_("Bar"))
class MyFormOne(forms.ModelForm):
class Meta:
model=MyModel
fields = ('field_foo', )
widgets = {'field_foo': forms.TextInput(attrs={'size': 10, 'maxlength': 50}),}
class MyFormTwo(MyFormOne):
class Meta(MyFormOne.Meta):
fields = MyFormOne.Meta.fields + ('field_foo2',)
widgets = MyFormOne.Meta.widgets
# Add new fields as a dictionary
widgets.update({'field_foo2': forms.TextInput(attrs={'size': 10, 'maxlength': 50}),)
# Or add them one by one
widgets['field_foo3'] = forms.TextInput(attrs={'size': 10, 'maxlength': 50})
Again, I have no idea if that will help you. Please let me know what results you get.
This snippet is a bit old, but it explains the only way I've ever been able to get form multiple inheritance to work. It's pretty ugly.
http://djangosnippets.org/snippets/703/
There is also an open ticket regarding it: https://code.djangoproject.com/ticket/7018