我正在尝试使用来自2个不同应用程序的3个模型之间的关系。当我尝试在Django中验证模型时,出现以下错误:
CommandError: One or more models did not validate:
tournaments.tournament: 'city' has a relation with model City, which has either not been installed or is abstract.
tournaments.tournament: 'sport' has a relation with model Sport, which has either not been installed or is abstract.
我已在项目中安装了所有应用程序和 django.contrib.sites ,如此处所示。
INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'location',
'sport',
'team',
'tournament',
)
试图改变列表中应用的顺序。
这是位置应用程序的模型。
from django.db import models
class Country(models.Model):
name = models.CharField('Country', max_length=200, unique=True)
abbr = models.CharField('Abbreviation', max_length=6, unique=True)
class Meta:
ordering = ('name',)
app_label = 'location'
verbose_name_plural = 'countries'
def __unicode__(self):
return self.name
class State(models.Model):
country = models.ForeignKey('Country', verbose_name=u'country')
name = models.CharField('State', max_length=200, unique=True)
abbr = models.CharField('Abbreviation', max_length=6, unique=True)
class Meta:
ordering = ('country', 'name')
unique_together = ('name', 'country')
app_label = 'location'
def __unicode__(self):
return self.name
class City(models.Model):
state = models.ForeignKey('State', verbose_name=u'state')
name = models.CharField('City', max_length=200, unique=True)
abbr = models.CharField('Abbreviation', max_length=6, unique=True)
class Meta:
ordering = ('state', 'name')
unique_together = ('name', 'state')
app_label = 'location'
verbose_name_plural = 'cities'
def __unicode__(self):
return self.name
这是运动应用程序的模型
from django.db import models
class Sport(models.Model):
name = models.CharField('Sport', max_length=200, unique=True)
abbr = models.CharField('Abbreviation', max_length=6, unique=True)
class Meta:
ordering = ('name',)
app_label = 'sport'
db_table = 'sports'
def __unicode__(self):
return self.name
最后锦标赛应用程序的模型
from django.db import models
from location.models import City
from sport.models import Sport
class Tournament(models.Model):
territory = (
(1, u'International'),
(2, u'National'),
(3, u'Regional'),
)
name = models.CharField('Tournament', max_length=200, unique=True)
abbr = models.CharField('Abbreviation', max_length=6, unique=True)
city = models.ForeignKey('City', verbose_name=u'city')
sport = models.ForeignKey('Sport', verbose_name=u'sport')
regional = models.PositiveIntegerField(u'Status', default=1, choices=territory)
start = models.DateTimeField('Start Date')
end = models.DateField('End Date')
def __unicode__(self):
return self.name
class Meta:
ordering = ('name',)
unique_together = ('name', 'city', 'regional')
app_label = 'tournament'
我已经阅读了很多os帖子,以及Django的项目文档,但我找不到什么错误。
答案 0 :(得分:0)
首先,您是否已完成python manage.py syncdb
和/或在所有应用上运行South迁移?
您是否尝试过更改这些行
city = models.ForeignKey('City', verbose_name=u'city')
sport = models.ForeignKey('Sport', verbose_name=u'sport')
到此?
city = models.ForeignKey('location.City', verbose_name=u'city')
sport = models.ForeignKey('location.Sport', verbose_name=u'sport')