在 Django Chosen Forms 的模型选择字段中列出各种模型属性
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
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)
这里我有两种形式,其中一种我想在下拉菜单中显示所有样本 ID。另一个我想显示所有样本名称。
对于示例 ID,这很容易,因为我已将 unicode 方法定义为 return 示例 ID。但是,我不知道如何调整模型、表单或视图以创建包含示例名称的下拉菜单(即模型属性,而不是 return 在 unicode 方法中编辑的属性。)
如何允许模型选择字段显示 unicode 方法中定义的模型属性以外的模型属性?
或者,我是否应该定义 unicode 方法,以便根据特定条件它 return 是特定模型属性。
我应该提一下,这里我使用的是 django chosen forms,它的行为与 Django 模型表单非常相似,只是增加了一些功能。
我使用以下代码得到的具体错误消息是:
__init__() takes exactly 1 argument (2 given)
我不确定你的 ChosenModelChoiceField
,我假设它是 ModelChoiceField
的子类,你应该覆盖字段上的 label_from_instance
方法:
class BlahChoiceField(chosenforms.ChosenModelChoiceField):
def label_from_instance(self, obj):
# return whatever text you want
return obj.sample_name
然后你做的定义:
sample_id = BlahChoiceField(queryset=Sample.objects.all())
编辑:
您的原始代码在正确的路径上,但是您的代码在这一行是错误的:
self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(sample_tuple)
应该是:
self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(choices=sample_tuple)
你可以这样做。
在你的模型中"Sample"定义一些方法:
@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
然后您的表格将是:
class SampleNameLookupForm(forms.Form):
sample_name = chosenforms.ChosenChoiceField(choices=Sample.name_choices())
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
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)
这里我有两种形式,其中一种我想在下拉菜单中显示所有样本 ID。另一个我想显示所有样本名称。
对于示例 ID,这很容易,因为我已将 unicode 方法定义为 return 示例 ID。但是,我不知道如何调整模型、表单或视图以创建包含示例名称的下拉菜单(即模型属性,而不是 return 在 unicode 方法中编辑的属性。)
如何允许模型选择字段显示 unicode 方法中定义的模型属性以外的模型属性?
或者,我是否应该定义 unicode 方法,以便根据特定条件它 return 是特定模型属性。
我应该提一下,这里我使用的是 django chosen forms,它的行为与 Django 模型表单非常相似,只是增加了一些功能。
我使用以下代码得到的具体错误消息是:
__init__() takes exactly 1 argument (2 given)
我不确定你的 ChosenModelChoiceField
,我假设它是 ModelChoiceField
的子类,你应该覆盖字段上的 label_from_instance
方法:
class BlahChoiceField(chosenforms.ChosenModelChoiceField):
def label_from_instance(self, obj):
# return whatever text you want
return obj.sample_name
然后你做的定义:
sample_id = BlahChoiceField(queryset=Sample.objects.all())
编辑:
您的原始代码在正确的路径上,但是您的代码在这一行是错误的:
self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(sample_tuple)
应该是:
self.fields['Sample_Name'] = chosenforms.ChosenChoiceField(choices=sample_tuple)
你可以这样做。
在你的模型中"Sample"定义一些方法:
@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
然后您的表格将是:
class SampleNameLookupForm(forms.Form):
sample_name = chosenforms.ChosenChoiceField(choices=Sample.name_choices())