Consider the following code:
class MyManyToManyField(models.ManyToManyField):
def __init__(self,*args,**kwargs):
field_name = ?!?
kwargs["related_name"] = "%(app_label)s.%(class)s." + field_name
super(MetadataManyToManyField,self).__init__(*args,**kwargs)
class MyModelA(models.Model):
modelAField = MyManyToManyField("MyModelB")
class MyModelB(models.Model):
pass
Is there any way for me to access the name of the field from within my overloaded init function? I want the related_name of modelAField to wind up being "MyAppName.MyModelA.modelAField".
I've thought about just passing it as a kwarg:
modelAField = MyManyToManyField("MyModelB",related_name="modelAField")
and then using it in init:
field_name = kwargs.pop("related_name",None)
if not field_name:
raise AttributeError("you have to supply a related name!")
But I'm hoping for something a bit nicer.
Thanks.