1👍
✅
Do not specify the default
value in this case. The best place to do that is in the model form save
method:
class TimeLogForm(forms.ModelForm):
class Meta:
exclude = ['begin_sec']
model = TimeLog
def __init__(self, *args, **kw):
super(TimeLogForm).__init__(self, *args, **kw)
def save(self, *args, **kw):
instance = super(TimeLogForm, self).save(commit=False)
last_entry = TimeLog.objects.all().order_by('-id')[0]
if last_entry:
instance.begin_sec = last_entry.begin_sec
else:
# this is the very first record do something
pass
instance.save()
return instance
Also you don’t need to set auto_now=False
and auto_now_add=False
for both fields, by default they are False
.
Source:stackexchange.com