Search code examples
pythonhtmldjangodjango-modelstuples

Tuples in Django models


I want to write the whole word on the website when entering the product information by entering the first letter of the status field. But here it receives the information correctly, but only displays the first letter on the web page.

class Product(models.Model):
    choice = (
        ('d', 'Dark'),
        ('s', 'Sweet'),
    )
    user = models.CharField(max_length=64)
    title = models.CharField(max_length=64)
    description = models.TextField()  
    category = models.CharField(max_length=64)      
    first_bid = models.IntegerField()  
    image = models.ImageField(upload_to="img/", null=True)      
    image_url = models.CharField(max_length=228, default = None, blank = True, null = True)
    status = models.CharField(max_length=1, choices= choice)
    active_bool = models.BooleanField(default = True)

    def __str__(self):
         return self.get_status_display()

This is one of the ways to display this field.

<a><a>Status: </a>{{ product.status }}</a><br>

Solution

  • Simply use {{ product.get_status_display }} and jinja will call the method automatically and display it' value.

    In a view context you will use product.get_status_display(). Check out the Django documentation for it.