gpt4 book ai didi

python - "No action selected"删除对象时说Django-admin

转载 作者:行者123 更新时间:2023-11-28 18:24:30 25 4
gpt4 key购买 nike

我创建了一个包含几个字段的简单模型 Product,然后转到 admin.py。我已经注册了 Product,使一些字段 list_editable 并创建了一个新操作 duplicate

def duplicate(modeladmin, request, queryset):
number = int(request.POST['number'])
product = queryset.first()
for i in xrange(number):
product.id = None
product.save()

class DuplicateActionForm(ActionForm):
number = forms.IntegerField()

class ProductAdmin(admin.ModelAdmin):
list_display = ('id','name','color','memory','ga_url','gs_url',)
list_editable = ('color','memory','name','ga_url','gs_url',)
action_form = DuplicateActionForm
# actions = [duplicate,]

admin.site.register(Product,ProductAdmin)

ProductAdmin 类的 actions 属性未被注释时,我可以复制对象。问题是我无法删除它们。当我检查行并选择 delete selected 时,它显示:No action selected

这是线路造成的:

action_form = DuplicateActionForm

因为如果 actions = [duplicate,] 被注释,我无法正确删除对象,直到我注释 action_form = DuplicateActionForm

你知道问题出在哪里吗?

最佳答案

您应该在自定义表单字段上添加 required=False。之后一切都会按预期工作。

class DuplicateActionForm(ActionForm):
number = forms.IntegerField(required=False)

关于python - "No action selected"删除对象时说Django-admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42374193/

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