Django一對多關係模型Add a related_name argument to the definit的錯誤

pythontab發表於2013-06-27

下面是一對多的關係模型

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')

   #...


相關文章