Is it possible set field size on Model?
Is it possible write a field size on Model?
This is part of my form.py, I would like do something like it on my model: email = forms.EmailField(label = "Email",widget=forms.TextInput(attrs={'size':'60',)
I can´t define this attribute on Form because I use classes relation. If I decla开发者_Python百科re "email" on my parent class and my child class won use this field, it will show it (the model exclude wont work because "email" attribute was declared on the parent)
models.py
class UserProfile(models.Model):
nome = models.CharField('Nome Completo',max_length=100 )
endereco = models.CharField('Endereço',max_length=100)
...
forms.py
class **cadastrousuarioForm**(formulariocadastrocompleto):
nome = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
...
class atualizacaoousuarioForm(**cadastrousuarioForm**):
class Meta (cadastrousuarioForm):
model = UserProfile
fields = ("endereco",)
exclude = ("nome")
...
No, field size cannot be defined on the model. The issue you describe with exclude not working on the child when the field is defined on the parent is actually an open bug in Django. In a sense, your frustration is warranted, but unfortunately, you don't have many options.
You might want to try a mix-in:
class MyFieldMixIn(forms.ModelForm):
my_field = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'}))
class ParentForm(forms.ModelForm):
# Common functionality, fields, etc. here
class ReplacementParentForm(ParentForm, MyFieldMixIn):
pass
class ChildForm(ParentForm):
# Child-specific stuff here
Then instead of using the parent form you use the replacement, so you get that field only on that form.
精彩评论