Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Map a Django model field to some user defined value

Hi My project works on Django and some AngularJS. I want map my Django model field values into some custom string values. The following is my Model,

class History(models.Model):
    TYPE_CHOICES = (
        (1, 'CREATE'),
        (0, 'EDIT'),
        (2, 'DELETE'),
    )
    user = models.ForeignKey(User, related_name='+')
    mw = models.ForeignKey('CP', related_name="history")
    field_name = models.CharField(max_length=192, null=False)
    old_value = models.CharField(max_length=500, null=True, blank=True)
    new_value = models.CharField(max_length=500, null=True, blank=True)
    type = models.IntegerField(default=0, choices=TYPE_CHOICES)
    date_created = models.DateTimeField(auto_now_add=True)

the type 1, 2 and 0 I need to display in my website. So i just passed the value as <td>{{t.type}}</td> But it giving me the values as 1, 2 and 0. How can I display it as create, update or delete string values? Any idea guys? Thanks in advance.

like image 315
vellattukudy Avatar asked Mar 14 '23 06:03

vellattukudy


1 Answers

In your template change {{t.type}} to {{ t.get_type_display }}.

You can read about this in docs: get_FOO_display docs

like image 115
Dima Kudosh Avatar answered Mar 16 '23 18:03

Dima Kudosh