简体   繁体   English

以Django选择的形式在``模型选择''字段中列出各种模型属性

[英]List Various Model Attributes in Model Choice Field in Django Chosen Forms

Models.py 型号

from django.db import models

class Sample(models.Model):
    sample_ID = models.CharField(max_length=20)
    sample_name = models.CharField(max_length=30)


    def __unicode__(self):
        return self.sample_ID

    class Meta:
        ordering = ['id']

Forms.py Forms.py

from django import forms
from chosen import forms as chosenforms
from .models import Sample

class SampleLookupForm(forms.Form):
    Sample_ID = chosenforms.ChosenModelChoiceField(queryset=Sample.objects.all())

class SampleNameLookupForm(forms.Form):
    def __init__(self):
        samples = Sample.objects.all().values_list('sample_name', flat=True)
        sample_tuple = [(i, i) for i in samples]
        self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(sample_tuple)

Here I have two forms, one of which I would like to show all sample IDs in the drop down menu. 在这里,我有两种形式,其中一种是我想在下拉菜单中显示所有样品ID。 The other I would like to show all of the sample names. 另一个我想显示所有样本名称。

For the Sample IDs, this is easy because I have defined the unicode method to return the sample ID. 对于样本ID,这很容易,因为我已经定义了unicode方法来返回样本ID。 However, I have no clue how to adjust the model, form, or view to create a drop down menu that contains the sample names (ie a model attribute other than that returned in the unicode method.) 但是,我不知道如何调整模型,窗体或视图以创建包含样本名称(即unicode方法返回的模型属性以外的模型属性)的下拉菜单。

How do I allow for model choice fields to display model attributes other than that defined in the unicode method? 我如何允许模型选择字段显示除unicode方法中定义的模型属性以外的模型属性?

Or, should I define the unicode method such that depending on certain conditions it returns a certain model attribute. 或者,我应该定义unicode方法,以便根据某些条件返回某个模型属性。

I should mention, here I am using django chosen forms which behaves very similarly to Django model forms, just with some added functionality. 我应该提到的是,这里我使用的是django选择的表单 ,其行为与Django模型表单非常相似,只是具有一些附加功能。

The specific error message I am getting with the following code is: 我通过以下代码得到的特定错误消息是:

__init__() takes exactly 1 argument (2 given)

I'm not sure about your ChosenModelChoiceField , I'm assuming it's a subclass of ModelChoiceField , you should overwrite label_from_instance method on the field: 我不确定您的ChosenModelChoiceField ,我假设它是ModelChoiceField的子类,您应该在该字段上覆盖label_from_instance方法:

class BlahChoiceField(chosenforms.ChosenModelChoiceField):
    def label_from_instance(self, obj):
        # return whatever text you want
        return obj.sample_name

Then for the definition you do: 然后为定义做:

sample_id = BlahChoiceField(queryset=Sample.objects.all())

django doc . django文件

Edit : 编辑

Your original code is on the right path, but your code is wrong on this line: 您的原始代码在正确的路径上,但此行的代码错误:

self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(sample_tuple)

It should be: 它应该是:

self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(choices=sample_tuple)

You could do something like this. 你可以做这样的事情。

In your model "Sample" define some methods: 在模型“样本”中定义一些方法:

@staticmethod
def name_choices():
    return [s.name_choice() for s in Sample.objects.all()]

def name_choice(self):
    return self.sample_name, self.sample_name

And then your form would be: 然后您的表格将是:

class SampleNameLookupForm(forms.Form):
    sample_name =  chosenforms.ChosenChoiceField(choices=Sample.name_choices())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM