Issue
I am trying to include a field that is a property
in the model_to_dict fields
list.
class Person(models.Model):
thumbnail = models.ImageField(upload_to='bla...')
...snipped...
@property
def thumbnail_url(self):
return self.thumbnail.url
def toJSON(self):
return model_to_dict(self, fields=[..., 'thumbnail_url',...])
When I call Person.toJSON() it does not include thumbnail_url
field.
What is wrong?
Thank you.
Solution
This is my current fix. I am open to another approach that works cleaner with model_to_dict()
@property
def thumbnail_url(self):
return self.thumbnail.url
def toJSON(self):
return {
**model_to_dict(self, fields=['all', 'my', 'fields']),
**{'thumbnail_url': self.thumbnail_url }
}
Answered By - kvothe__
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.