python - Multiple foreign key fields in abstract Django class -
i have abstract base class declares 2 foreign key fields user model:
class basemodel(models.model): updated = models.datetimefield(null=true) updated_by = models.foreignkey(settings.auth_user_model, null=true, related_name="updated_by") created = models.datetimefield(null=true) created_by = models.foreignkey(settings.auth_user_model, null=true, related_name="created_by") class meta: abstract=true
i have multiple classes inherit class. when run makemigrations
, following error each possible class-pair , both created_by
, updated_by
:
myapp.classa.updated_by: (fields.e305) reverse query name 'classb.updated_by' clashes reverse query name 'classb.updated_by'.
hint: add or change related_name argument definition 'classa.updated_by' or 'classb.updated_by'.
even though have related_name set. works fine 1 of 2 foreign key fields declared.
is possible have 2 foreign key fields same model in abstract class, , if so, how set up?
this expected behavior mentioned in documentation.
to work around problem, when using related_name in abstract base class (only), part of name should contain
'%(app_label)s'
,'%(class)s'
.
class basemodel(models.model): updated = models.datetimefield(null=true) updated_by = models.foreignkey(settings.auth_user_model, null=true, related_name="updated%(app_label)s_%(class)s_related") created = models.datetimefield(null=true) created_by = models.foreignkey(settings.auth_user_model, null=true, related_name="created%(app_label)s_%(class)s_related") class meta: abstract=true
Comments
Post a Comment