use django How do I change the value of true or false to a text value type it I am checking the row in the entire table
| name item | type items | active |
|---|---|---|
| item1 | 15.0 | True |
| item2 | 15.0 | False |
change values row active use jquery or django file view.py:
| name item | type items | active |
|---|---|---|
| item1 | 15.0 | displayed |
| item2 | 15.0 | stope |
help please
CodePudding user response:
The most elegant way is likely to specify choices for the BooleanField, so:
from django.db import models
ACTIVE_CHOICES = [
(False, 'stope')
, (True, 'displayed')
]
class MyModel(models.Model):
# …
active = models.BooleanField(choices=ACTIVE_CHOICES)
Then you can render the corresponding label in a template with:
{{ mymodelobject.get_active_display }}
It will also use these choices by default in a ModelForm, ModelAdmin, and serializers.
CodePudding user response:
Without code, it is difficult to give a precise answer.
but the easiest way to set value based on a boolean in javascript is to use a ternary operator.
If true, active will be set to "displayed", if false, active will be set to "stope"
active = boolean_value ? "displayed" : "stope"
