rnk
rnk

Reputation: 2204

How to avoid this dropdown combo box?

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.

Upvotes: 0

Views: 276

Answers (1)

Brian Neal
Brian Neal

Reputation: 32399

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()

Upvotes: 3

Related Questions