如何避免这个下拉组合框? [英] How to avoid this dropdown combo box?

查看:106
本文介绍了如何避免这个下拉组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了这样的播放列表和项目的模型:

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

我的播放列表视图:

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.

谢谢。

推荐答案

也许您需要明确地包含或排除要向用户显示的字段

然后,您必须手动更新您将对象保存到数据库之前排除的对象的部分。看看 commit = False 参数 ModelForm.save()方法为在文档中描述

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

这篇关于如何避免这个下拉组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆