Django ModelMultipleChoiceField没有显示正确的选择名称 [英] Django ModelMultipleChoiceField not displaying correct Choice names

查看:128
本文介绍了Django ModelMultipleChoiceField没有显示正确的选择名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的postgres db上的视图创建多项选择字段,但是网页显示不正确。具体来说,虽然它提供了正确的选择数量,但它并没有通过 check_name字段命名它们,而是将每个选择都命名为检查对象。这是我的代码:

I am attempting to create a Multiple Choice Field from a view on my postgres db, however the webpage is displaying incorrectly. Specifically, while it provides the correct number of selections it does not name them by the "check_name" field, instead it names every selection 'Check object'. Here's my code:

models.py

class Check(models.Model):
    pkey = models.AutoField(primary_key=True)
    cif = models.CharField(max_length=255)
    check_name = models.CharField(max_length=255)
    description = models.TextField()

    class Meta:
        managed = False
        db_table = 'precheck_check'

forms.py

class ProcessFileForm(forms.Form):
    checks_to_run = forms.ModelMultipleChoiceField(
        queryset = Check.objects.all(),
        to_field_name = "check_name",
        widget = forms.CheckboxSelectMultiple,
        )

views.py

def successful_upload(request):
    if request.method == 'POST':
        form = ProcessFileForm(request.POST, user=request.user)
        if form.is_valid():
            return render(request, 'precheck/checks_successful.html')
    else:
        form = ProcessFileForm()
    return render(request, 'precheck/select_checks.html',{'form':form})

值得注意的是,我正在从名为 precheck_check的视图中提取数据在postgres db中。似乎可以正确看到该视图,因为它为我提供了正确的选择数量。

Of note, I am pulling the data from a view named 'precheck_check' in the postgres db. It seems to be seeing the view correctly since it is giving me the correct number of choices.

推荐答案

最简单的解决方法是添加 __ str __ 方法用于您的 Check 模型。

The simplest fix is to add a __str__ method to your Check model.

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible  # only if you need to support Python 2
class Check(models.Model):
    pkey = models.AutoField(primary_key=True)
    cif = models.CharField(max_length=255)
    check_name = models.CharField(max_length=255)
    description = models.TextField()

    class Meta:
        managed = False
        db_table = 'precheck_check'

    def __str__(self):
        return self.check_name

如果要显示与 __结果不同的值str __ 方法,您可以将 ModelMultipleChoiceField 并覆盖 label_from_instance

If you want to display a different value than result of the __str__ method, you can subclass ModelMultipleChoiceField and override label_from_instance.

from django import forms

class MyModelMultipleChoiceField(forms.ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.check_name

然后使用以下格式的字段:

Then use the field in the form:

class ProcessFileForm(forms.Form):
    checks_to_run = forms.MyModelMultipleChoiceField(...)

这篇关于Django ModelMultipleChoiceField没有显示正确的选择名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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