Django Admin Actions on single object

前端 未结 4 857
春和景丽
春和景丽 2021-01-01 22:41

The admin actions seem to work on several items selected in the list view of django admin interface:

In my case I would like to have a simple action button on the ch

4条回答
  •  没有蜡笔的小新
    2021-01-01 22:48

    The built-in admin actions operate on a queryset.

    You can use a calable for the action you whant or to show something else:

    class ProductAdmin(admin.ModelAdmin):
        list_display ('name' )
        readonly_fields('detail_url)
    
    
    def detail_url(self, instance):
        url = reverse('product_detail', kwargs={'pk': instance.slug})
        response = format_html("""{0}""", product_detail)
        return response
    

    or using forms

    class ProductForm(forms.Form):
        name = forms.Charfield()
    
        def form_action(self, product, user):
            return Product.value(
                id=product.pk,
                user= user,
            .....
    
            )
    
    
    @admin.register(Product)
    class ProductAdmin(admin.ModelAdmin):
    
    # render buttons and links to
    def product_actions(self, obj):
            return format_html(
                'Action1 '
                'Action 2',
                reverse('admin:product-action-1', args=[obj.pk]),
                reverse('admin:aproduct-action-3', args=[obj.pk]),
            )
    

    for more details about using forms

提交回复
热议问题