In the docs for get_or_create()[1], it states that any argument given  
with `__` is stripped for the create() call. The reasons for this  
should be obvious. Thus, your original code was trying to save an  
empty Rating.

As for speed, the new version has less SQL. `user__pk` creates a join  
and then compares the id of the user table with the id you gave. The  
second version is the same as `user_id=request.user.id` because that’s  
how Django compares models.

[1]: 
http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-methods-that-do-not-return-querysets

On Aug 21, 2008, at 10:43 AM, Burr Settles wrote:

>
> When the IntegrityError is thrown, the debug package stated that
> "songs_rating.user_id may not be NULL". It is indeed being thrown by
> get_or_create(). Strangely enough, request.user.id contains the
> correct value of the logged in user's ID, so I don't know what the
> problem is. I did manage to get the view working by swapping out the
> following for line 6:
>
>   rating = Rating.objects.get_or_create(user=request.user, song=song)
> [0]
>
> Now, it matches against the request.user and song objects directly, as
> opposed to their IDs as primary keys. I don't know what sort of effect
> this has on the efficiency of the underlying DB hit, but it seems the
> best I can do for now.
>
> --B
>
>
> On Aug 21, 12:20 am, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
>> On Wed, Aug 20, 2008 at 5:13 PM, Burr Settles  
>> <[EMAIL PROTECTED]> wrote:
>>
>>> I recently came back to a project I put on hold for a couple weeks,
>>> and after updating the Django trunk, all my references to
>>> "request.user.id" are broken. Here is an example view for a rating
>>> widget:
>>
>>> 1 @login_required
>>> 2 def rate(request, song_id):
>>> 3     song = get_object_or_404(Song, pk=song_id)
>>> 4     if 'rating' not in request.GET or request.GET['rating'] not in
>>> range(1,5):
>>> 5         HttpResponseRedirect(song.get_absolute_url())
>>> 6     rating =  
>>> Rating.objects.get_or_create(user__pk=request.user.id,
>>> song__pk=song.id)
>>> 7     rating.rating = int(request.GET['rating'])
>>> 8     rating.save()
>>> 9     return HttpResponseRedirect(song.get_absolute_url())
>>
>>> This throws an "IntegrityError" if the user is logged in, presumably
>>> complaining that request.user.id is NULL (at line 6).
>>
>> Presumably?  I suspect you are guessing wrong here, but it would be  
>> best to
>> make sure.  There's lots of information available on the deubg page  
>> when
>> something like an IntegrityError is thrown, have you checked the  
>> values of
>> local variables to see what request.user.id actually is?
>>
>>> But it passes
>>> the @login_required constraint just fine. I'm having similar  
>>> problems
>>> with other views that use request.user.
>>
>>> Does anyone know if the user API has recently changed? I've scoured
>>> the latest online documentation, and can't seem to find any  
>>> alternate
>>> way to access the ID of the currently logged in user. Any help is
>>> appreciated.
>>
>> I do not think anything has changed with request.user.  It seems  
>> more likely
>> something is going wrong in get_or_create.  Such as the get fails  
>> to find an
>> existing record so it tries to insert one but that is violating an  
>> integrity
>> constraint.   What, exactly, does the IntegrityError message say?   
>> Usually
>> it will give some indication of what it is objecting to, which  
>> could provide
>> a clue.
>>
>> Karen
> >

-- 
Peter of the Norse

-- 
Peter of the Norse


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to