我正在使用Django 2.0
我在actions
界面
admin
def clear_arbitrase(modeladmin, request, queryset):
queryset.update(arbitrase_generated=False)
@admin.register(WallmartRecord)
class WallmartRecordAdmin(admin.ModelAdmin):
actions = [
clear_arbitrase
]
这会更新arbitrase_generated
False
WallmartRecord
模式的arbitrase
标记。
我有另一张表来保存ArbitraseRecord
ArbitraseRecord
的记录
我希望在将arbitrase_generated
标记为False
时从post_save
删除记录。
我尝试在models.py
@receiver(post_save, sender=WallmartRecord)
def post_save_wallmart_record_receiver(sender, instance, *args, **kwargs):
if not instance.arbitrase_generated:
# delete records from ArbitraseRecord
arbitrase_record = ArbitraseRecord.objects.filter(wallmart_record=instance)
if arbitrase_record is not None:
for record in arbitrase_record:
record.delete()
信号
<state>
<name>Name state 2</name>
<x>x</x>
<y>y</y>
<width>w</width>
<height>h</height>
<nlinks>2</nlinks>
<effect>1</effect>
<signal>
<name>Signal 1</name>
<amplitude>3</amplitude> <!--parameter 3 -->
<duration>10</duration> <!--input 0 -->
<listOfPoints>
<point>0</point>
<point>1</point>
<point>10</point>
<point>0</point>
</listOfPoints>
</signal>
</state>
但这似乎不是叫接收者。
如何在管理操作选择中删除其他模型的记录?
答案 0 :(得分:0)
update()
直接在SQL级别执行,并且它不会调用模型的save()
,因此在您的情况下不会触发post_save
信号。您可以将删除移到clear_arbitrase
,如下所示:
def clear_arbitrase(modeladmin, request, queryset):
queryset.update(arbitrase_generated=False)
ArbitraseRecord.objects.filter(wallmart_record__id__in=queryset.values_list('id', flat=True)).delete()