If I have the following model in django;
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100,default=name)
How do I make the fullname field default to name? As it is right now, the fullname defaults to the string representation of the name CharField.
Example:
new MyModel(name='joel')
would yield ‘joel’ as both name and fullname, whereas
new MyModel(name='joel',fullname='joel karlsson')
would yield different name and fullname.
I wonder if you’re better off doing this via a method on your model:
Perhaps, instead of
display_namethis should be your__unicode__method.If you really want to do what you’ve asked though, then you can’t do this using the
default– use thecleanmethod on your form instead (or your model, if you’re using new-fangled model validation (available since Django 1.2).Something like this (for model validation):
Or like this (for form validation):