Re: Populating a model field with an implicit default values with request data from the admin interface
Hi,
On Wed, Jan 27, 2010 at 4:30 AM, Ian <spidaman@gmail.com> wrote:
> Sorry about the noob questions, I'm digging through the docs... that
> was perfect, thanks!
You're welcome, but I have less than a month of Django experience
myself, so take my help with a grain of salt :)
> Only issue was: I had to set the created_by and updated_by fields to
> blank=True in the model to defeat the validator, even though the
> semantics are that they shouldn't be blank (seems hacky, is there a
> better way?).
I suspect that you might be looking to do something like this:
class Article(models.Model):
title = models.CharField(max_length=31)
blurb = models.CharField(max_length=255)
created_by = models.CharField(max_length=31, editable=False)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_by = models.CharField(max_length=31, editable=False)
updated_at = models.DateTimeField(auto_now=True, editable=False)
class ArticleAdmin(models.ModelAdmin):
def save_model(self, request, obj, form, change):
if change:
obj.updated_by = request.user.username
else:
obj.created_by = request.user.username
obj.save()
So the timestamps would be set and updated automatically, and you set
the created_by/updated_by fields depending on whether the form
submission was an add or a change. One possible catch is that
editable=False means that those fields would not show up in the
article's form (and so blank=True would not be required, if I remember
correctly), though you can make them show up in the article listing in
the admin.
Regards,
Eugene Wee
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home