4👍
✅
There are two ways you can do this.
1) Model method:
class Something(models.Model):
field1 = models.CharField()
def to_dict(self):
return self.__dict__
def __str__(self):
return self.field1
and then you can call {{ instance.to_dict }}
in your template.
2) Template filter
@register.filter
def to_dict(instance):
return instance.__dict__
and then you can call {{ instance|to_dict }}
Note: You must activate the template filters and create them by following these instructions
- [Django]-I want to represent django model as table in html page with table heading same as django field name. How can I implement this?
- [Django]-Django REST: What's the recommended approach for supporting nested collections in URLs, such as /users/5/snippets/
- [Django]-Process_template_response doesn't get called in Django
- [Django]-Unable to understand a line from the django tutorial
Source:stackexchange.com