如何在模型中使用外键在过滤器上构建下拉列表

时间:2019-02-08 18:36:37

标签: python django

我正在基于可用的记录选项构建一个带有动态列表的模型(下面的列表将在单独的表中)

我尝试了几种选择,并继续出现以下错误:

“ CategoryCodes.active_ID :(字段E004)'选择'必须是可迭代的(例如列表或元组)。”

ALL = '0000'
SYSTEM = '9000'
BUSINESS = '9002'
INDIVIDUAL = '9005'
PROJECT = '9006'

PREFIX_TYPE_CHOICES = (
    (ALL, '- All -'),
    (SYSTEM, 'System'),
    (BUSINESS, 'Business'),
    (INDIVIDUAL, 'Individual'),
    (PROJECT, 'Project')
)

Class TypeOptions(models.Model):

type_ID = models.UUIDField(default=uuid.uuid4, editable=False)

type_code = models.AutoField('Code', primary_key=True, editable=False)
type_code_name = models.CharField('Code name', max_length=200, default="")
type_code_default = models.BooleanField('Code default', default=False)
datastructure_prefix_name = models.CharField('Segment',
                                             max_length=4, choices=PREFIX_TYPE_CHOICES, default=ALL)

datastructure_name = models.CharField(
    'Table name', max_length=50, default="")

datastructure_element_name = models.CharField(
    'Table element', max_length=50, default="")

#
#  Date/Time records was created
createDateTime = models.DateTimeField(auto_now_add=True)

#
#  Date/Time records was updated
updateDateTime = models.DateTimeField(auto_now=True)

#
#  Active options the owner list
active_ID = models.CharField('Active state',
                             max_length=3, choices=ACTIVE_TYPE_CHOICES, default=ACTIVE)

#
#  This defines the sorting order
class Meta:
    ordering = ('datastructure_prefix_name', 'type_code')

类别CategoryCodes(models.Model):

SCCID = models.UUIDField(
    primary_key=True, default=uuid.uuid4, editable=False)

state_code = models.CharField(max_length=5)
state_code_name = models.CharField(max_length=100)
state_code_description = models.CharField(max_length=100)

#
#  Date/Time records was created
createDateTime = models.DateTimeField(auto_now_add=True)

#
#  Date/Time records was updated
updateDateTime = models.DateTimeField(auto_now=True)

def get_option_list():
    return get_user_model().objects.filter(datastructure_prefix_name='ALL', datastructure_name='ALL', datastructure_element_name='active_ID')

def get_default():
    return get_user_model().objects.filter(datastructure_prefix_name='ALL', datastructure_name='ALL', datastructure_element_name='active_ID', type_code_default=True)

#
#  Active options the owner list
active_ID = models.ForeignKey('system.TypeOptions', choices=models.SET(
    get_option_list), default=models.SET(get_default), on_delete=models.PROTECT)

我希望获得一个列表,该列表会根据过滤条件动态加载到选项active_ID中

1 个答案:

答案 0 :(得分:0)

我从没在choices中使用过ForeignKey参数,但是您遇到的错误是因为...

功能:

def get_option_list():
    return get_user_model().objects.filter(datastructure_prefix_name='ALL', datastructure_name='ALL', datastructure_element_name='active_ID')

不返回列表或元组,而是查询集。

您可能想在该函数中使用QuerySet.values_list来构建正确的数据。

也许:

def get_option_list():
    users = get_user_model().objects.filter(datastructure_prefix_name='ALL', datastructure_name='ALL', datastructure_element_name='active_ID')
    return users.values_list('id', 'username')