Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:24:47+00:00 2026-06-03T05:24:47+00:00

I’m developing Django application, and I have following error ‘Category’ object has no attribute

  • 0

I’m developing Django application, and I have following error

'Category' object has no attribute '_state'

My models are

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 = ""

In a new blog post, the categories are taken as comma separated value, and if a new category is encountered it is added to the database.
Similarly for Tags.

I am not clear about this _state thing, could you please point me in the right direction.
Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:24:49+00:00Added an answer on June 3, 2026 at 5:24 am

    You’re not running Model.__init__. You must do so.

    At the very least, you need to have a line like super(Category, self).__init__() inside Category.__init__.

    In practice, you have far more important design problems. You should not include the class name in attributes; it should be Category.name, not Category.CategoryName. Category.__init__ should use keyword arguments, not its own special arguments. The description should have blank=True instead of null=True. You don’t need Category.__init__.

    Here is a slightly tidier version of what you’re doing:

    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
    

    This is still ugly, though. Firstly and most importantly, tags and category (renamed to categories) should be relations, not plain text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.