D
D
dimas75252018-08-02 11:29:39
Django
dimas7525, 2018-08-02 11:29:39

How to display category name?

There is a field that allows you to select a vehicle category.

TYPE_CHOICES = (
        ('leg', u"Легковое ТС"),
        ('gruz', u"Грузовое ТС"),
    )

type = models.CharField(max_length=5, blank=True, null=True, default="m",  choices=TYPE_CHOICES)

How to display the Russian name of the category in the template, and not the abbreviation that is stored in the database

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim Shatalov, 2018-08-02
@dimas7525

https://docs.djangoproject.com/en/2.1/ref/models/i...
No need to reinvent the wheel

A
Artyom Belousov, 2018-08-02
@flygrounder

Make a method or property. Something like the following:

@property
def char_type(self):
     for i in TYPE_CHOICES:
         if i[0] == self.type:
             return i[1]

In the template it would look like
{{ model.char_type }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question