Let's say I have the following Django model:
class Parent(models.Model):
name = models.CharField(max_length=50)
children = models.ManyToManyField("Child")
class Child(models.Model):
name = models.CharField(max_length=50)
I have the Parent
and Child
models stored in variables in a script. Given these two models, how can I dynamically get the field name children
as a string:
def get_field_name(model, related_model):
# what do I need here
get_field_name(Parent, Child)
# "children"