首页 > 后端开发 > Python教程 > Django Add a related_name argument to the definit

Django Add a related_name argument to the definit

高洛峰
发布: 2016-10-17 14:14:30
原创
1343 人浏览过

下面是一对多的关系模型

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:\python27\lib\site-packages\django\core\management\commands\runserver.py”, line 48, in inner_run

self.validate(display_num_errors=True)

File “c:\python27\lib\site-packages\django\core\management\base.py”, line 253, in validate

raise CommandError(“One or more models did not validate:\n%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')

   #...


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板