“对象没有属性'_state'”错误

时间:2012-05-07 01:40:36

标签: python django

我正在开发Django应用程序,我有以下错误

'Category' object has no attribute '_state'

我的模特

class Blog(models.Model):
    BlogMeta = models.CharField(max_length=200)
    BlogTitle = models.CharField(max_length=100)
    BlogContent = models.TextField()
    BlogCategory = models.CharField(max_length=300)
    BlogTags = models.CharField(max_length=300)
    BlogDate = models.DateField()
    def __unicode__(self):
        return self.BlogTitle
    def save(self):
        self.BlogDate = datetime.datetime.now()
        Categorylist = re.findall(r'\w+',self.BlogCategory)
        TagList = re.findall(r'\w+', self.BlogTags)
        #Get already existing tags and category
        dbCategoryList = Category.objects.all()
        dbTagsList = Tags.objects.all()
        clisflag = False
        tlisflag = False
        #check if categories and tags in new blog exists previously or not.
        for clis in Categorylist:
            for dbclis in dbCategoryList:
                if (clis == dbclis.CategoryName):
                    clisflag = True
                    break
                else:
                    continue

            if not clisflag:
                createCategory = Category(clis)
                createCategory.save()
            clisflag = False

        for tlis in TagList:
            for dbtlis in dbTagsList:
                if(tlis == dbtlis.TagName):
                    tlisflag = True
                    break
                else:
                    continue
            if not tlisflag:
                createTag = Tags(tlis)
                createTag.save()
            tlisflag = False

class Tags(models.Model):
    TagName = models.CharField(max_length=20)
    TagDesc = models.TextField(null=True)
    def __unicode__(self):
        return self.TagName
    def __init__(self,name):
        self.TagName = name
        self.TagDesc = ""
class Category(models.Model):
    CategoryName = models.CharField(max_length=20)
    CategoryDesc = models.TextField(null=True)
    def __unicode__(self):
        return self.CategoryName
    def __init__(self, name):
        self.CategoryName = name
        self.CategoryDesc = ""

在新的博文中,类别被视为逗号分隔值,如果遇到新类别,则会将其添加到数据库中。 类似的标签。

我不清楚_state这个问题,请你指点我正确的方向。 非常感谢!

1 个答案:

答案 0 :(得分:2)

您没有运行Model.__init__。你必须这样做。

至少,您需要super(Category, self).__init__()内的Category.__init__行。

在实践中,您有更重要的设计问题。您不应该在属性中包含类名;它应该是Category.name,而不是Category.CategoryNameCategory.__init__应该使用关键字参数,而不是它自己的特殊参数。说明应为blank=True而不是null=True。您不需要Category.__init__

这是你正在做的一个稍微整洁的版本:

class Blog(models.Model):
    meta = models.CharField(max_length=200)
    title = models.CharField(max_length=100)
    content = models.TextField()
    categories = models.CharField(max_length=300)
    tags = models.CharField(max_length=300)
    date = models.DateField()

    def __unicode__(self):
        return self.title

    def save(self):
        self.date = datetime.datetime.now()
        category_list = re.findall(r'\w+', self.categories)
        tag_list = re.findall(r'\w+', self.tags)
        #check if categories and tags in new blog exists previously or not.
        db_categories = Category.objects.all()
        for clis in category_list:
            for dbclis in db_categories:
                if clis == dbclis.name:
                    break
            else:
                new_category = Category(name=clis)
                new_category.save()

        db_tags = Tags.objects.all()
        for tlis in tag_list:
            for dbtlis in db_tags:
                if tlis == dbtlis.name:
                    break
            else:
                new_tag = Tags(name=tlis)
                new_tag.save()


class Tags(models.Model):
    name = models.CharField(max_length=20)
    desc = models.TextField(blank=True)
    def __unicode__(self):
        return self.name


class Category(models.Model):
    name = models.CharField(max_length=20)
    desc = models.TextField(blank=True)
    def __unicode__(self):
        return self.name
但是,这仍然是丑陋的。首先也是最重要的是,tagscategory(重命名为categories)应该是关系,而不是纯文本。