Auto-populating created_by and actionDate field with Django admin site
I have a model like this:
class Tour(models.Model):
Name=models.CharField(max_length=100)
Count=models.SmallIntegerField()
ActionDate=models.DateTimeField(editable=False)
ActionUser=models.ForeignKey(User,editable=False)
StatusType=models.ForeignKey(StatusType)
now I wanna auto populate current user in my Tour,so I used this code in admin.py:
def save_model(self, request, obj, form, change):
instance = form.save(commit=False)
instance.ActionUser = request.user
instance.save()
form.save_m2m()
return instance
admin.site.register(Tour,TourAdmin)
it work great and it auto polulate current User in my Tour Table,but now I can't save current date in ActionDate field,I added this code to save_model but it cause error:
self.ActionDate=datetime.datetime.today()
then I 开发者_如何学运维tryed to override save in my Tour Model :
def save(self,*args,**kwargs):
self.ActionDate=datetime.datetime.today()
super(Tour, self).save(*args,**kwargs)
but this cause error,too. what should I do to auto populate both ActionUser and ActionDate? tnx in advance
Why don't you use auto_now
in the field definition?
精彩评论