Reputation: 1495
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?
Upvotes: 0
Views: 3236
Reputation: 15310
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.
Upvotes: 4