我有一个Django应用程序,并希望在用户的配置文件中显示多个选项复选框.然后他们将能够选择多个项目.
这是我的models.py的简化版本:
from profiles.choices import SAMPLE_CHOICES
class Profile(models.Model):
user = models.ForeignKey(User, unique=True, verbose_name_('user'))
choice_field = models.CharField(_('Some choices...'), choices=SAMPLE_CHOICES, max_length=50)
Run Code Online (Sandbox Code Playgroud)
我的表格类:
class ProfileForm(forms.ModelForm):
choice_field = forms.MultipleChoiceField(choices=SAMPLE_CHOICES, widget=forms.CheckboxSelectMultiple)
class Meta:
model = Profile
Run Code Online (Sandbox Code Playgroud)
和我的views.py:
if request.method == "POST":
profile_form = form_class(request.POST, instance=profile)
if profile_form.is_valid():
...
profile.save()
return render_to_response(template_name, {"profile_form": profile_form,}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
我可以看到POST只发送一个值:
choice_field u'choice_three'
Run Code Online (Sandbox Code Playgroud)
当地的vars params正在发送一份清单:
[u'choice_one', u'choice_two', u'choice_three']
Run Code Online (Sandbox Code Playgroud)
所有表单字段都显示正确,但是当我提交POST时,我收到错误
错误绑定参数7 - 可能不受支持的类型.
我是否需要在视图中进一步处理多选字段?模型字段类型是否正确?任何帮助或参考将不胜感激.
在Django Admin中,有没有办法隐藏"Auth"部分?假设这是一个没有应用程序的Django和准系统项目的干净安装,只有管理员.
我在我的项目中使用django-threadedcomments和django-voting来实现类似Reddit的评论投票系统.
我已经正确设置了所有内容,并且我能够成功记录每个线程评论及其子项的投票,但是我对如何对评论进行排序感到困惑,以便评分最高的评论上升到顶部.
通过模板标签会是一个解决方案吗?我试过这个并返回了一个列表,其中的项目按score降序排序,但是评论的父子关系搞砸了.这是我做的:
class OrderByVotesNode(template.Node):
def __init__(self, queryset_var, direction="desc"):
self.queryset_var = template.Variable(queryset_var)
self.direction = direction
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
try:
direction = template.Variable(self.direction).resolve(context)
except template.VariableDoesNotExist:
direction = "desc"
model = value.model
qn = connection.ops.quote_name
ctype = ContentType.objects.get_for_model(model)
by_score = model.objects.filter(id__in=[f.id for f in value]).extra(select={"score": """
SELECT coalesce(SUM(vote), 0 )
FROM %s
WHERE content_type_id = %s
AND object_id = %s.%s
""" % (qn(Vote._meta.db_table), ctype.id, qn(model._meta.db_table), qn(model._meta.pk.attname))},
order_by=[(direction == "desc" and "-" or "") + …Run Code Online (Sandbox Code Playgroud)