1. ホーム
  2. django

[解決済み] Django のエラーメッセージ "Add a related_name argument to the definition" (定義に related_name 引数を追加してください

2023-06-14 13:21:05

質問

D:\zjm_code\basic_project>python manage.py syncdb
Error: One or more models did not validate:
topics.topic: Accessor for field 'content_type' clashes with related field 'Cont
entType.topic_set'. Add a related_name argument to the definition for 'content_t
ype'.
topics.topic: Accessor for field 'creator' clashes with related field 'User.crea
ted_topics'. Add a related_name argument to the definition for 'creator'.
topics.topic: Reverse query name for field 'creator' clashes with related field
'User.created_topics'. Add a related_name argument to the definition for 'creato
r'.
topicsMap.topic: Accessor for field 'content_type' clashes with related field 'C
ontentType.topic_set'. Add a related_name argument to the definition for 'conten
t_type'.
topicsMap.topic: Accessor for field 'creator' clashes with related field 'User.c
reated_topics'. Add a related_name argument to the definition for 'creator'.
topicsMap.topic: Reverse query name for field 'creator' clashes with related fie
ld 'User.created_topics'. Add a related_name argument to the definition for 'cre
ator'.

どのように解決するのですか?

django が一意な名前を生成できない外部キーがいくつもあります。

モデルの外部キーフィールドの定義に "related_name" という引数を追加することで、手助けすることができます。 例えば

content_type = ForeignKey(Topic, related_name='topic_content_type')

詳しくはこちらをご覧ください。 http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name