gpt4 book ai didi

python - 如何限制 Django admin 中的选择数量

转载 作者:行者123 更新时间:2023-11-30 22:28:38 26 4
gpt4 key购买 nike

假设我有两个模型,GroupPerson,其中 GroupPerson 的外键字段。在管理页面中,组表示为人员管理的下拉列表/选择字段。现在,我希望将选项的数量限制为五个,并且应根据它们的名称进行排序。

目前,我有以下代码:

class PersonAdmin(admin.ModelAdmin):
form = PersonAdminForm
...
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
if db_field.name == 'group':
kwargs['queryset'] = Group.objects.all().order_by('name')[:5]

return super(PersonAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs)

class PersonAdminForm(forms.ModelForm):
class Meta:
model = Person

问题是,当我尝试保存对象时,出现以下错误:AssertionError: 获取切片后无法过滤查询。

我搜索了该错误并发现了很多 SO 线程,但它们似乎都没有帮助我解决我的问题。

这是完整的堆栈跟踪:

Traceback (most recent call last):
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 583, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 206, in inner
return view(request, *args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1453, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
return bound_func(*args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/db/transaction.py", line 394, in inner
return func(*args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1396, in changeform_view
if form.is_valid():
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/forms.py", line 162, in is_valid
return self.is_bound and not bool(self.errors)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/forms.py", line 154, in errors
self.full_clean()
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/forms.py", line 353, in full_clean
self._clean_fields()
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/forms.py", line 368, in _clean_fields
value = field.clean(value)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/fields.py", line 150, in clean
value = self.to_python(value)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/forms/models.py", line 1185, in to_python
value = self.queryset.get(**{key: value})
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/db/models/query.py", line 345, in get
clone = self.filter(*args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/man/.virtualenvs/foo/local/lib/python2.7/site-packages/django/db/models/query.py", line 703, in _filter_or_exclude
"Cannot filter a query once a slice has been taken."
AssertionError: Cannot filter a query once a slice has been taken.

最佳答案

您可以在init方法中修改查询集,找到前五个组ids,然后通过__in过滤pk > 查找

class PersonAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PersonAdminForm, self).__init__(*args, **kwargs)
group_ids = Group.objects.all().order_by('name').values_list('pk', flat=True)[:5]
self.fields['group'].queryset = Group.objects.filter(pk__in=group_ids).order_by('name')

group = forms.ModelChoiceField(queryset=None, empty_label=None)

class Meta:
model = Person
fields = '__all__'

关于python - 如何限制 Django admin 中的选择数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564705/

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