Django一对多关系模型Add a related_name argument to the definit的错误

Django一对多关系模型Add a related_name argument to the definit的错误[Python框架]

下面是一对多的关系模型

class Cats(models.Model):

   #…

   catnum = models.IntegerField(unique=True)

   #…

class Items(models.Model):

   catid = models.ForeignKey(Cats, to_field='catnum', db_column='catid')

   #…

Unhandled exception in thread started by

Traceback (most recent call last):

File “c:python27libsite-packagesdjangocoremanagementcommandsunserver.py”, line 48, in inner_run

self.validate(display_num_errors=True)

File “c:python27libsite-packagesdjangocoremanagementase.py”, line 253, in validate

raise CommandError(“One or more models did not validate:
%s” % error_text)

django.core.management.base.CommandError: One or more models did not validate:

beauty.items: Reverse query name for field ‘catid’ clashes with field ‘Cats.items’. Add a related_name argument to the definit

ion for ‘catid’.

发生的错误大概意思是要增加一个related_name参数,所以Items模型改为

class Items(models.Model):

   catid = models.ForeignKey(Cats, to_field='catnum', db_column='catid', related_name='catid')

   #…

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Django一对多关系模型Add a related_name argument to the definit的错误