【问题标题】:ERROR :- One or more models did not validate错误:- 一个或多个模型未验证
【发布时间】:2014-03-07 14:24:57
【问题描述】:

我是 django 的新手。 我的问题是:- 应用 syncdb 时出现错误(如下所述)

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

我的模型.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'

请帮忙!

【问题讨论】:

  • sorry:- 我也导入了from django.db import models from django.contrib.auth.models import User from datetime import datetime的库
  • practicalArea 应该继承自models.Model。它应该可以解决您的问题。
  • 感谢@KamilRykowski :)

标签: django django-models


【解决方案1】:

您的 practiceArea 类也应该是 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()

【讨论】:

  • 非常感谢,这是我的错误。我今天很困惑,所以我忽略了这一点(我的英语不太好)。再次感谢:)
  • 不客气。这也是您了解related_name 用途的最佳时机。你应该这样声明它:practical_area = models.ForeignKey('PracticalArea', related_name='profiles'),如果你有一个 PracticalArea 实例pa = PracticalArea.objects.all()[0],你可以像这样引用它的配置文件:pa.profiles.all() 而不是pa.practical_Area.all()
【解决方案2】:

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

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

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

【讨论】:

  • 如果我使用模型名称而不是字符串,我得到一个新错误:AssertionError: ForeignKey() is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self' 正如我提到的。
  • 你的 PersonArea 类必须继承 models.Model
猜你喜欢
  • 2013-02-13
  • 1970-01-01
  • 2011-02-06
  • 2013-04-06
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多