Home >Backend Development >Python Tutorial >Django Add a related_name argument to the definit

Django Add a related_name argument to the definit

高洛峰
高洛峰Original
2016-10-17 14:14:301331browse

下面是一对多的关系模型

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-packagesdjangocoremanagementcommandsrunserver.py”, line 48, in inner_run

self.validate(display_num_errors=True)

File “c:python27libsite-packagesdjangocoremanagementbase.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')

   #...


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn