I have a method inside a model class, and I want to get the display value of a choice field in such model, but I get a weird value.
This is the code:
class Actor(models.Model):
PERSON = '0'
ROLE = '1'
AUTO = '2'
TYPE_CHOICES = (
(PERSON, 'Person'),
(ROLE, 'Role'),
(AUTO, 'Auto'),
)
alias = models.CharField(max_length=10, default='', unique=True)
name = models.CharField(max_length=255, default='')
email = models.EmailField(null=True, blank=True)
actor_type = models.CharField(max_length=1, choices=TYPE_CHOICES, default=PERSON)
long_description = models.TextField(max_length=long_description_max_length, default='', null=True, blank=True)
def get_list_fields(self):
list_fields = {
'id' : self.id,
'name' : self.alias,
'description' : self.name,
'extra_fields' : "[{}][{}]".format(
self.email,
getattr(self, 'get_actor_type_display')
)
}
return list_fields
And this is the result i get:
{'id': 1,
'name': 'hugo',
'description': 'Hugo Luis Villalobos Canto',
'extra_fields': '[hugo@utopia-software.net][functools.partial(>, field=)]'
}
I don't know what that functools.partial(>, field=)
stands for, and I don't know how to get the result I want: the display value of the current content of the field.
Thanks for your help.
def get_list_fields(self):
list_fields = {
'id' : self.id,
'name' : self.alias,
'description' : self.name,
'extra_fields' : "[{}][{}]".format(
self.email,
self.get_actor_type_display()
)
}
return list_fields
try this
You need add parenthesis to call the get_display
function :
def get_list_fields(self):
list_fields = {
'id' : self.id,
'name' : self.alias,
'description' : self.name,
'extra_fields' : "[{}][{}]".format(
self.email,
getattr(self, 'get_actor_type_display()')
)
}
return list_fields
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.