[Answered ]-Custom Django admin action, remove entries related by ForeignKey

2👍

Assuming queryset is a set of A’s, you should be able to loop over them and delete the ‘b_set’ (reverse FK lookup) of each, like so:

def clear_something(modeladmin, request, queryset):
    queryset.update(field=0)
    for a in queryset:
        a.b_set.all().delete()

Alternatively, use B’s model manager:

def clear_something(modeladmin, request, queryset):
    queryset.update(field=0)
    for a in queryset:
        B.objects.filter(f=a).delete()

Leave a comment