错误: - 一个或多个模型未验证

时间:2014-02-10 10:03:53

标签: django django-models

我是django的新手。 我的问题是: - 应用syncdb

时出现错误(下面描述)
  

错误:一个或多个模型未验证:

my models.py

     class practicalArea:
        practical_area = models.CharField(max_length=25)

        class Meta:
            db_table = 'practical_area'

        def __unicode__(self):
            return self.practical_area


class Profile(models.Model):
        auth_user_id = models.ForeignKey(User, related_name='userProfile')
        address = models.TextField(blank=True)        
        state = models.CharField(max_length=20)
        practical_area = models.ForeignKey('practicalArea', related_name='practical_Area')        
        company = models.CharField(max_length=40)     
        photo = models.CharField(max_length=250)
        created_date = models.DateTimeField()
        modify_date = models.DateTimeField()


        class Meta:
            db_table = 'adminPanal_profile'


        def save(self):
            if self.created_date == None:
                self.created_date = datetime.now()
            self.modify_date = datetime.now()
            super(Profile, self).save()

如果我将“practical_area = models.ForeignKey('practicalArea',related_name ='practical_Area')”更改为“practical_area = models.ForeignKey(practicalArea,related_name ='practical_Area')”

然后我收到了错误:

  

AssertionError:ForeignKey()无效。 ForeignKey的第一个参数必须是模型,模型名称或字符串'self'

请帮忙!

2 个答案:

答案 0 :(得分:1)

您的practicalArea类也应该是models.Model的子类。试试这样:

class PracticalArea(models.Model):
    practical_area = models.CharField(max_length=25)

    class Meta:
        db_table = 'practical_area'

    def __unicode__(self):
        return self.practical_area


class Profile(models.Model):
    auth_user_id = models.ForeignKey(User, related_name='userProfile')
    address = models.TextField(blank=True)        
    state = models.CharField(max_length=20)
    practical_area = models.ForeignKey('PracticalArea', related_name='practical_Area')        
    company = models.CharField(max_length=40)     
    photo = models.CharField(max_length=250)
    created_date = models.DateTimeField()
    modify_date = models.DateTimeField()


    class Meta:
        db_table = 'adminPanal_profile'


    def save(self):
        if self.created_date == None:
            self.created_date = datetime.now()
        self.modify_date = datetime.now()
        super(Profile, self).save()

答案 1 :(得分:0)

你应该直接给出Model Name,而不是在字符串中给出它,因为如果你在字符串中给出模型名称,django期望竞争路径到模型类。

    practical_area = models.ForeignKey(practicalArea, related_name='practical_Area')       

正如您为用户所做的那样。