I want to know what models are children of a model to retrieve their on_delete
property. As I know, like below, if we have ownerModel
which is parent of childModel1
and check1Model
:
import uuid
from django.db import models
class ownerModel(models.Model):
ownerId = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False, blank=True)
class check1Model(models.Model):
checkId = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False, blank=True)
owner=models.ForeignKey(ownerModel,on_delete=models.CASCADE)
class childModel1(models.Model):
childId = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False, blank=True)
check2=models.ForeignKey(ownerModel,on_delete=models.CASCADE)
Then we can get what models are children of ownerModel
with a code like this:
class myView(views.APIView):
def get(self, request, format=None):
for f in ownerModel._meta.get_fields():
if 'field' in f.__dict__.keys():
print('***childModels***')
print(f.__dict__)
print()
return Response({'0000'}, status=status.HTTP_200_OK)
I mean by checking if the field
key is in __dict__.keys()
in items of ownerModel._meta.get_fields()
.
Of course, here we get extended info about children models:
***childModels***
{'field': <django.db.models.fields.related.ForeignKey: owner>, 'model': <class 'Users.models.ownerModel'>, 'related_name': None, 'related_query_name': None, 'limit_choices_to': {}, 'parent_link': False, 'on_delete': <function
CASCADE at 0x00000286550848B0>, 'symmetrical': False, 'multiple': True, 'field_name': 'ownerId', 'related_model': <class 'Users.models.check1Model'>, 'hidden': False}
***childModels***
{'field': <django.db.models.fields.related.ForeignKey: check2>, 'model': <class 'Users.models.ownerModel'>, 'related_name': None, 'related_query_name': None, 'limit_choices_to': {}, 'parent_link': False, 'on_delete': <function CASCADE at 0x00000286550848B0>, 'symmetrical': False, 'multiple': True, 'field_name': 'ownerId', 'related_model': <class 'Users.models.childModel1'>, 'hidden': False}
So I find these 2 conditions necessary to get child models info:
- In child models, making sure child relationship is set up with a line like below:
models.ForeignKey(ownerModel,on_delete=models.CASCADE)
- As said "if the
field
key is in__dict__.keys()
in items ofownerModel._meta.get_fields()
" to get children info.
But the problem is that in some cases I can't get the children info from parent model. So:
- It makes me wonder if these 2 conditions are enough to find out which models are children of a model?
- Are there other similar ways to get which models are children of a model?
By the way, I want to have on_delete
also and having on_delete
is the only reason I am using _meta.get_fields()
over _meta.fields
because _meta.fields
doesn't provide on_delete
property.
This is my code if you wanna have a look. Note that in full answer I also wanna know what has made problem. So in this case, that __dict__.keys()
doesn't provide items which don't have field
in their keys (doesn't provide child model details). Because generally those 2 conditions provide child model details. So later I can get child model details in all codes.
the problem is that even with for f in ownerModel._meta.get_fields(include_hidden=True)
and without any further if
s doesnt retrieve lines including on_delete
properties in this project. but in the other projects ownerModel._meta.get_fields()
provides them. and I don't whats the cause that sometimes ownerModel._meta.get_fields()
provides these relationships infos and other times doesnt.