related_name argument not working as expected in Django model?

前端 未结 2 1047
一向
一向 2021-02-01 03:24

I recently got a ForeignKey clash in my Django model. I have the need to have two foreign keys (owner, assigned_to) ultimately pointing to the same model (a user).

From

相关标签:
2条回答
  • 2021-02-01 03:31

    If you are using related_name in abstract base class you need to use a '%(app_label)s' and '%(class)s' in it. Its mentioned in django doc

    Be careful with related_name

    0 讨论(0)
  • 2021-02-01 03:46

    If you have ForeignKey relationships in an abstract base class every class inheriting from it will have this relationship. As a result of this you must not 'hardcode' its related_name, because all sub classes will try to create the same accessor on the realted class (TaskUser in this case).

    You should better do something like:

    owner = models.ForeignKey(TaskUser, related_name="%(app_label)s_%(class)s_ownership")
    

    See the django docs on this.

    0 讨论(0)
提交回复
热议问题