hi,
I am more or less a beginner with django and databases.I was trying to
set the author field  of an entry from the request.user instead of
letting the user select from a drop down list of users.I designed the
models like

class Entry(models.Model):
    pub_date=models.DateField(default=date.today)
    description=models.TextField()
    author=models.ForeignKey(User)

class EntryForm(ModelForm):
    class Meta:
        model=Entry
        exclude = ('author',)

In views.py I created an add_entry function,

def add_entry(request,template_name,page_title):
    form_data=get_form_data(request)
    form=EntryForm(form_data)
    if request.method=='POST' and form.is_valid() :
        newentry=form.save()
        ....some other ops...
        newentry.author=request.user
        newentry.save()
        ....

However ,when I tried a testcase using

def test_add_entry(self):
    self.client.login(username='me',password='mypass')
    self.post_data={
 
'pub_date':'2010-03-08',
                        'description':'my new
entry'
                        }
 
response=self.client.post(reverse('myapp_add_entry'),self.post_data)

I get this IntegrityError, saying that 'null value in column
"author_id" violates not-null constraint'
The error occurs at  the first occurrence of line
newentry=form.save() in the add_entry()method

Can someone help me figure out how to add the author value correctly?
since I am using client.login() shouldn't the author field be taken
from the logged in user thru request.user?

thanks
jim

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

Reply via email to