1👍
If this is an object-by-object attribute, you could store it as an attribute of the model itself and access it with instance.__class__.locale. You could also do type(instance).locale or instance._meta.model.locale
class Dog(models.Model):
name = models.CharField(max_length=127, default='rover')
cost = models.IntegerField()
locale = "uk"
class Cat(models.Model):
name = models.CharField(max_length=127, default='fluffy')
cost = models.IntegerField()
locale = "us"
Or if able to access it from the instance itself, it’s even simpler: instance.locale
If there’s added functionality you’re hoping to get out of i18n (perhaps in the template) you could pass in a context variable or store that as an attribute of a class-based view.
1👍
Unfortunately I don’t see how you could use i18n to resolve your problem.
Alternatively, this doesn’t use Django i18n but it should work
You could set this up in your settings.py
:
CURRENCY = {
'uk': '£',
'us': '$',
}
Then to get the correct currency you can do:
settings.CURRENCY[object.locale]
You could even go further and wrap this in a model property
@property
def currency(self):
return settings.CURRENCY[self.locale]
- [Answered ]-Custom User profile for Django user
- [Answered ]-403 error for new files posted through django admin
- [Answered ]-Annotate causes ProgrammingError: must appear in the GROUP BY clause or be used in an aggregate function
- [Answered ]-Try to join a OneToOne relationship in Django