Monday, June 3, 2013

Re: Weird behavior of a modified_by and created_by field

I don't know why, a look at your admin.py might tell us.  But since you are storing user in a CharField (rather than a ForeignKey) you should assure that you get the representation that you want by using something like request.user.username instead of just request.user (which is a model instance, not a string).


On Sat, Jun 1, 2013 at 9:55 AM, Victor <vdemart@gmail.com> wrote:
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)
    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,

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')

    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()

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


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment