5

I'm using modelforms for getting playlist and its items. It also contains login script. I'm trying to set the currently logged in user to the user model. You can see this thing I've posted before How to avoid this dropdown combo box?

class playlistmodel(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=200)

    def __unicode__(self):
        return self.title

class itemsmodel(models.Model):
    playlist = models.ForeignKey(playlistmodel)
    item = models.TextField()

    def __unicode(self):
        return self.item

class playlistform(ModelForm):
    class Meta:
        model = playlistmodel
        exclude = {'user'}

class itemsform(ModelForm):
    class Meta:
        model = itemsmodel
        exclude = {'playlist'}

Here is the playlist view:

def playlistview(request):
    if request.method == 'POST':
        form = playlistform(request.POST)
        if form.is_valid():
                data = form.save(commit=False)
                data.user = request.user
                data.save()
                return render_to_response('playlist.html', {'data': data})
    else:
        form = playlistform()
        return render_to_response('playlist.html', {'form': form, 'user': request.user}, context_instance=RequestContext(request))

Playlist.html file:

https://gist.github.com/1576136

Error Page:

https://gist.github.com/1576186

But I'm getting ValueError:

Exception Type: ValueError Exception Value: Cannot assign "<django.utils.functional.SimpleLazyObject object at 0x7f0234028f50>": "playlistmodel.user" must be a "User" instance

Traceback: Local vars --- data.user = request.user

Here is my settings.py https://gist.github.com/1575856

Thank you.

Community
  • 1
  • 1
rnk
  • 2,174
  • 4
  • 35
  • 57
  • 1
    Just in case, verify you have the default [TEMPLATE_CONTEXT_PROCESSORS](https://docs.djangoproject.com/en/1.3/ref/settings/#template-context-processors) in your `settings.py` – César Jan 07 '12 at 15:05
  • Which version of Django are you using? Try `data.user_id = request.user.id` instead. – Alasdair Jan 07 '12 at 15:18
  • @César I've posted the link for settings.py on my question section. – rnk Jan 07 '12 at 20:08
  • @rnk, I order to debug your application can you insert a print request.user at begin of view and before assignement? Please, post results. – dani herrera Jan 07 '12 at 20:40
  • @César Look at my updates on my question section. I'm getting Integrity error reg_playlistmodel.user_id may not be NULL Traceback: data = form.save() – rnk Jan 07 '12 at 21:42
  • @César But the output in the html file shows me the correct logged in user. – rnk Jan 07 '12 at 21:43
  • Silly test, but what type of object do you see if you were to add a print statement in your view? `data.user = request.user; print data.user, type(data.user)` . We could at least make sure you are getting a proper User model instance in your request. – jdi Jan 08 '12 at 18:02

3 Answers3

5

I know this post is old, but if anyone gets here with the same problem, the answer is that request.user is actually a wrapper for django's auth.user. So request.user is a SimpleLazyObject, and it's purpose is avoiding unnecessary instantiation, and also implementing a simple user caching mechanism. To access the actual user (and instantiate it, when accessing the first time), you need to do:

auth.get_user(request)

This will give you an instance of auth.user. If you need more detail on what's going on inside, see this post.

Community
  • 1
  • 1
Anoyz
  • 7,431
  • 3
  • 30
  • 35
0

I have the very same problem, but it only arises when no user is logged in (expecting an AnonymousUser instance). At least when my superuser is logged in, I found no problem.

Despite I have not found a solution yet, maybe you'll find a clue in this answer

Well, now I've realised that in my case AnonymousUser should be stored as null in the db, but leave this answer to post the clue link (have no permission yet to leave comments).

Hope it helps!

Community
  • 1
  • 1
viridis
  • 177
  • 10
0

difficult to tell, but i would try this in your playlistview:

form = playlistform(request, request.POST, instance=playlistmodel)

maybe you can ommit the last parameter (instance=playlistmodel), because this would only be in need if you change an existing object

Hope this helps...

Jingo
  • 3,200
  • 22
  • 29