Django cascade delete on 2 foreign keys of the same table

I have a model A and model B. Model B has 2 foreign keys to model A as shown. There is a 1 to many relationship between Model A to B such that a dataset (A) can have multiple data elements(B).

Model A: id (primary key) dataset_id dataset_version dataset_name

Model B: id (primary key) dataelement_id dataelement_version dataelement_name fk_dataset_id fk_dataset_version

My question is: How to cascade delete a row from Model A such that the deletion occurs on Model B using fk_dataset_id and fk_dataset_version together ? Currently, django runs 2 separate queries to delete data from Model B on columns fk_dataset_id and fk_dataset_version but I want the deletion query to use an AND condition for deletion for e.g. DELETE from model B where B.fk_dataset_id = 1 AND B.fk_dataset_version = 3

Iam using a django rest framework with ModelViewSet for both Model A and B

Back to Top