gpt4 book ai didi

python - 尝试让 optgroup 标 checkout 现在 Django 管理内联中

转载 作者:行者123 更新时间:2023-12-01 04:29:32 25 4
gpt4 key购买 nike

这是迄今为止我认为可行的方法,但我收到了此错误:

Template error

In template /usr/lib/python2.7/site-packages/django/contrib/admin/templates/admin/edit_inline/tabular.html, error at line 50
Caught TypeError while rendering: render_option() takes exactly 4 arguments (23 given)
40 {% endspaceless %}
41 </td>
42 {% for fieldset in inline_admin_form %}
43 {% for line in fieldset %}
44 {% for field in line %}
45 <td class="{{ field.field.name }}">
46 {% if field.is_readonly %}
47 <p>{{ field.contents }}</p>
48 {% else %}
49 {{ field.field.errors.as_ul }}
50 {{ field.field }} <--- error here

admin.py中的代码

def get_construct_choices():
construct_request_choices = Construct.objects.all().order_by('family','promotor','additional_mutation')
construct_request_choices = itertools.groupby(construct_request_choices, key=lambda x:str(x.family))
choices = []
for family, group in construct_request_choices:
choices.append((family, [str(val) for val in group]))
print choices
return choices

class ConstructRequestCustomForm(forms.ModelForm):
class Meta:
model = ConstructRequest
construct = forms.ChoiceField(choices=get_construct_choices())

class ConstructRequestInline(admin.TabularInline):
model = ConstructRequest
form = ConstructRequestCustomForm
extra = 1

class RequestAdmin(make_DefaultAdminAuditTable(Request)):
inlines = (ConstructRequestInline,)

最佳答案

如果您使用 choices 的命名组,每个组的第二个元素应该是 2 元组的可迭代。文档中的示例是:

CHOICES = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
)

在你的代码中,你正在做

    choices.append((family, [str(val) for val in group]))

所以看起来第二个元素是字符串列表,而不是二元组列表。如果你这样做,它可能会起作用

    choices.append((family, [(str(val), str(val)) for val in group]))

或者,如果您希望显示值与数据库中存储的值不同,您可能需要稍微更改它。

最后,由于 get_construct_choices 访问数据库,因此最好在表单的 __init__ 方法中设置选项。否则,选择将在代码加载时加载,但不会更新。

class ConstructRequestCustomForm(forms.ModelForm):
class Meta:
model = ConstructRequest

def __init__(self, *args, **kwargs):
super(ConstructRequestCustomForm, self).__init__(*args, **kwargs)
self.fields['construct'].choices = get_construct_choices()

关于python - 尝试让 optgroup 标 checkout 现在 Django 管理内联中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com