I have the following model where created_by or modified_by (user) are defined as CharField.
models.py
class Item(models.Model):
barcode = models.CharField(primary_key=True, max_length=20)
description = models.CharField(max_length=255)
barcode = models.CharField(primary_key=True, max_length=20)
description = models.CharField(max_length=255)
created_date = models.DateTimeField(editable=False, auto_now_add=True,null=True, blank=True) # Field name made lowercase.
modified_date = models.DateTimeField(editable=False,auto_now=True,null=True, blank=True) # Field name made lowercase.
created_by = models.CharField(max_length=15,null=True,blank=True)
modified_by = models.CharField(max_length=15,null=True,blank=True)
AND,
modified_date = models.DateTimeField(editable=False,auto_now=True,null=True, blank=True) # Field name made lowercase.
created_by = models.CharField(max_length=15,null=True,blank=True)
modified_by = models.CharField(max_length=15,null=True,blank=True)
AND,
According to what I read in the internet (see http://stackoverflow.com/questions/4754485/dry-way-to-add-created-modified-by-and-time) in admin.py I redefined save_model as follows:
admin.py
class ItemOption(admin.ModelAdmin):
search_fields=['barcode','description']
list_display = ('barcode','description','created_by', 'modified_by')
fields=('barcode','description','created_by', 'modified_by')
search_fields=['barcode','description']
list_display = ('barcode','description','created_by', 'modified_by')
fields=('barcode','description','created_by', 'modified_by')
def save_model(self, request, obj, form, change):
if change:
obj.modified_by = request.user
obj.modified_date = datetime.now()
else:
obj.created_by = request.user
obj.created_date = datetime.now()
obj.save()
if change:
obj.modified_by = request.user
obj.modified_date = datetime.now()
else:
obj.created_by = request.user
obj.created_date = datetime.now()
obj.save()
Now it happens (see the attached png file) that - when a record is created in item model - in created_by you "correctly" find the username as a string, while - when a record is modified in item model - in modified_by you find the pk number of the user (which, by the side, is correct).
Why is that and how can I fix it in order to have the user as a string in modified_by?
Ciao
Vittorio
No comments:
Post a Comment