ValueError in modelforms

I use modelforms to get the playlist and its elements. It also contains a login script. I am trying to set the current user to a user model. You can see this thing that I posted earlier. How to avoid this combobox?

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 get 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 are my settings .py https://gist.github.com/1575856

Thank.

+5
source share
3 answers

, , - , , request.user django auth.user. , request.user SimpleLazyObject, - , . ( ), :

auth.get_user(request)

auth.user. , , . .

+5

, :

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

, (instance = playlistmodel), ,

, ...

0

, , ( AnonymousUser). , , .

, , ,

, , AnonymousUser null db, , ( ).

, !

0
source

All Articles