How to fill model field value from a different ModelForm field with some calculation?
I want to have age fiel开发者_开发百科d in my ModelForm and use it to fill birth_year in the model.
from datetime import datetime
import forms
from myapp.models import MyModel
class MyAgeForm(forms.ModelForm):
age = forms.IntegerField()
# define other custom fields here
class Meta:
model = MyModel
# probably define what fields from the model to include/exclude here
def save(self, *args, **kwargs):
self.instance.birth_year = datetime.now().year - self.cleaned_data['age']
return super(MyAgeForm, self).save(*args, **kwargs)
加载中,请稍侯......
精彩评论