Say I have a model like:
from django.db import models USER_TYPE_CHOICES = ( (1, 'Free'), (2, 'Paid'), ) class Account(models.Model): name = models.CharField(max_length=20) user_type = models.IntegerField(default=1, choices=TYPE_CHOICES)
and in a template I want to test the user_type to show a special section if the user is of type 'Paid'.
I'd like to do something similar to a C #define or constant to test user_type. So my template code would look like:
{% ifequal user_type PAID_ACCOUNT %}
instead of using a magic number like:
{% ifequal user_type 2 %}
What is the most elegant way to do this in Django? Should I just define a custom context processor with FREE_ACCOUNT and PAID_ACCOUNT in it? Perhaps a template tag?
Thank you!
score:6
Personally what I would do is add an is_paid(self)
method to your Account model which can then be called directly from the template. The added benefit is that this has utility in other parts of your application.
It also gives you the power to do extra processing in the function e.g. a staff member (is_staff
) might have their user type automatically set to paid.
Credit To: stackoverflow.com
Related Query
- Testing Django model choices in templates
- Django testing model with ImageField
- Model name of objects in django templates
- Python Django Templates and testing if a variable is null or empty string
- Passing arguments to model methods in Django templates
- Django Model - choices versus foreign key?
- Django model field choices - wouldn't a dict be better?
- Enumerating model choices in a Django Rest Framework serializer
- django translate model choices
- Same field, different choices in Django model subclasses
- How do I iterate over Django CHOICES in a template - without using a form or model instance
- Django Model Choices
- Django model - changing options in a choices field and migrate
- django testing - confirm that model is unique, depending on another field
- Check if Django model field choices exists
- django model choices make no effect on saved data
- Automatically add constants for each of the choices in a Django model
- Testing with a custom user model as a ForeignKey in Django 1.5
- Django testing for templates loaded via template tags
- Testing django model clean method
- Dynamically limit choices for Foreignkey in Django models based on another foreign key in the same model
- Django save a model instance when testing but can't find it in real database
- Django RadioSelect Choices From Model
- django use model choices in modelform
- Django templates how loop through rows of a model
- Django Model Choice Field: Huge List of choices
- Unit testing Django model with an image - Not quite understanding SimpleUploadedFile
- Django Testing Model Attributes
- For a django model field with choices, how to efficiently get a list of the choices that are used in the database?
- How to connect a ReactJS Form to a Django model to automatically obtain fields (foreignkeys, choices etc.)
More Query from same tag
- Django: Model name clash
- Django: Need help with DB schema (ManyToMany, ForeignKey relations)
- Jquery File Upload plugin in django
- Set object permissions so that only the user specified in `owner` field can modify the object?
- Django ModuleNotFoundError: No module named 'myProject'
- How to hide Python & Django technologies from Wappalyzer
- Is there a way to login in django rest api in browser without UI?
- Dynamic File Path in Django and South
- Create a view in django
- Django Rest Framework - return user id and token after registration
- django-sentry unit tests erroring out of the box
- Django Tutorial: name 'HttpResponse' is not defined
- celery 4.0.0 and Class based task workflow
- django admin form with predefined inlines
- Django implicit field naming for class with query expression - how does it work?