views:

33

answers:

2

I am making a blog application and want to automatically add the current user when i submit a new post through the admin site. Is there any way that i could detect the current logged-in user and add it to the post.
These are the models:

class Post(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField('Title', max_length=100)
    content = models.TextField('Content')
    comments_allowed = models.BooleanField('Allow Comments', default=True)
    time = models.DateTimeField('Time', auto_now_add = True)

And in the admin.py:

class PostAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['title']}),
        (None,               {'fields': ['user']}),
        (None,               {'fields': ['content']}),
        (None,               {'fields': ['comments_allowed']}),
    ]
    list_display = ('title','user', 'time','comments_allowed','id',)
    list_filter = ['time']
    search_fields = ['title']
    date_hierarchy = 'time'    
admin.site.register(Post,PostAdmin)
+1  A: 

It can be done.

Ignacio Vazquez-Abrams
thanks...sure will try this one. :)
dcrodjer
A: 

To detect current logged in user

http://docs.djangoproject.com/en/dev/topics/auth/

bobsr