0

I've created modelform of playlist and items like this:

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

class itemsform(ModelForm):
    class Meta:
            model = itemsmodel

My 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
                    a = data.save()
                    return render_to_response('playlist.html', {'data': a})
    else:
            form = playlistform()
            playlistmodel.user.id = request.user.id
            return render_to_response('playlist.html', {'form': form}, context_instance=RequestContext(request))

I've also set up with login script. If you need to create a playlist, you need to login and the problem is when I'm trying to create a playlist after logging in, it shows all the users in the dropdown combo box to select which user and enter the title of the playlist. What if I want to do to remove that useless dropdown box. It also shows for item page like selecting the playlist in the dropdown box and entering the items.

Thank you.

rnk
  • 2,174
  • 4
  • 35
  • 57

1 Answers1

3

Perhaps you need to explicitly include or exclude what fields you want displayed to the user.

You'll then have to manually update the parts of the object that you excluded before you save the object to the database. Take a look at the commit=False argument to the ModelForm.save() method as described in the documentation.

For example, your code might look something like this:

if form.is_valid():
    obj = form.save(commit=False)
    obj.user = request.user
    obj.save()
Brian Neal
  • 31,821
  • 7
  • 55
  • 59
  • That's fine I excluded the user field and the playlist field, but I'm getting Integrity Error when I'm creating the title of the playlist. Exception Value: reg_playlistmodel.user_id may not be NULL – rnk Jan 07 '12 at 02:17
  • How can I defaultly set the user for the above reg_playlistmode.user_id to the currently logged in user with request.user – rnk Jan 07 '12 at 02:20
  • I've updated my answer with a link to the docs that tell you how you can do that. – Brian Neal Jan 07 '12 at 02:41
  • @ Brian Neal: Thanks. But now it shows this valueError, Exception Type: ValueError Exception Value: Cannot assign "": "playlistmodel.user" must be a "User" instance. – rnk Jan 07 '12 at 02:54
  • You'll have to post your code and the traceback either here or maybe in another question. – Brian Neal Jan 07 '12 at 03:05
  • Traceback: Local vars --- data.user = request.user – rnk Jan 07 '12 at 07:29