views:

955

answers:

3

Ok, so here's the skinny:

# models.py
class Article( models.Model ):
    title           = models.CharField( max_length = 255 )
    author          = models.ForeignKey( User )
    published_at    = models.DateTimeField( auto_now_add = True )
    body            = models.TextField( )

    def __unicode__( self ):
        return self.title

# admin.py
from hpccoe.news.models import Article
from django.contrib import admin
from django import forms
from django.forms import widgets

class ArticleAdminForm( forms.ModelForm ):    
    title   = forms.CharField( max_length = 255, required = True )
    body    = forms.CharField( required = True, widget = widgets.Textarea )

class ArticleAdmin( admin.ModelAdmin ):
    fields  = [ 'title', 'body' ]
    form    = ArticleAdminForm

admin.site.register( Article, ArticleAdmin )

As you can see, I'm omitting the author field in the Admin form. I want to, somehow, auto-fill this before it's saved. How do I make that happen? I've been on Google for the last hour to no avail.

Thanks in advance.

+5  A: 
class ArticleAdmin( admin.ModelAdmin ):
    fields  = [ 'title', 'body' ]
    form    = ArticleAdminForm

    def save_model(self, request, obj, form, change):
         obj.author = "name Here"
         obj.save()
Mike
+5  A: 

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#save-model-self-request-obj-form-change

The hash location on this link seems broken; doesn't take me anywhere on the page.
Carl Meyer
+1  A: 

wow!!! i was looking for this :)

thanks

Asinox
By now you have enough reputation that you can just upvote the helpful answer and than delete this comment/"answer"...
sth