How to map a Django model field to a user-defined value

Hi My project is running on Django and some AngularJS. I want to map the values ​​of the fields of a Django model in some custom string values. Below 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)

Type 1, 2, and 0 I need to display on my website. So I just passed the value as <td>{{t.type}}</td>But it gave me the values ​​as 1, 2 and 0. How can I display it as creating, updating or deleting string values? Any ideas guys? Thank you in advance.

+4
source share
2 answers

, django , (angularjs), .

, , angular?

 <!-- if using angular js -->

{% verbatim %}
  {{ data.angular_t }}  {{ angularvariablescope }}
{% endverbatim %}

<!-- all django varibale  -->

{{t.type}} to  {{ t.get_type_display }}. {# django variable #}

model.py

@property
def angular_t(self)
    return self.get_type_display()

(tastypie)

class Resource(ModelResource):
    angular_t = fields.CharField(attributes='angular_t', null=True)
+1

{{t.type}} {{ t.get_type_display }}.

: get_FOO_display docs

+6

Source: https://habr.com/ru/post/1614582/


All Articles