0

I have a table in my database, region

class region(models.Model):
    region_name = models.CharField(primary_key=True,max_length=50, null=False)

    @classmethod
    def __unicode__(self):
        return "hello"

When I am trying to add several new instances of region into the DB through the admin interface, so the title of all the existing regions is "hello", so I update the __unicode__(self) function as

@classmethod
def __unicode__(self):
    return u'%s' %(self.region_name)

but now the admin displays

Exception Type: AttributeError
Exception Value:    
type object 'region' has no attribute 'region_name'

I have check my DB and there is region_name in the table for region. Can anyone help why this happens?

1 Answer 1

4

You shouldn't be using a @classmethod. Just remove the @classmethod decorator for the __unicode__ function; the first parameter to a function decorated with @classmethod is the class itself, and not the model instance for your region instance.

This answer provides a good overview of the purpose and use of classmethod and staticmethod function decorators in Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.