pm13
unread,Aug 5, 2008, 8:03:52 AM8/5/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django developers
I tried to generalized fields and exclude options from ModelForm to be
useful also for Form (for example for including and excluding fields
from possible superclasses or for ordering). And I have two questions
about this:
1. Fields which are not in models:
This example (django.contrib.auth.forms):
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given
username and password.
"""
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^\w+$',
help_text = _("Required. 30 characters or fewer. Alphanumeric
characters only (letters, digits and underscores)."),
error_message = _("This value must contain only letters,
numbers and underscores."))
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput)
class Meta:
model = User
fields = ("username",)
Option fields say - get everything from model, filter fields and add
everything from form. I think it would be better: get everything from
model and form and then filter fields - for example, what if there is
a useless field in a superclass?
class Meta:
model = User
fields = ("username", "password1", "password2")
2. Fields and exclude in the same form:
This example (modeltests.modelforms.models):
class Category(models.Model):
name = models.CharField(max_length=20)
slug = models.SlugField(max_length=20)
url = models.CharField('The URL', max_length=40)
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = ['name', 'url']
exclude = ['url']
It could mean two things (without reading of code I cannot say what is
right):
a) exclude 'url' and then include 'name' and 'url' - result is 'name'
and 'url'.
b) include 'name' and 'url' and then exclude 'url' - result is 'name'.
Option B is right, but it can be more simple written so:
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = ['name']
Why there are two different ways? Is it not better to forbid to use
fields and exclude at once?