python - Is it possible to order a Django Model using the active language? -


i'm working on django 1.5.1 , i'm wondering following question: possible define ordering in model on translatable field, using django-transmeta , using active language user accessing admin?

so far, achieved use translated field , order using default language, not active one, call get_language() returns default defined in settings.py if call method meta class (or modeladmin class).

here code i'm working on, , help:

class startuptype(models.model):      __metaclass__ = transmeta      name = models.charfield(_("name"), max_length=30, unique=true)     slug = models.slugfield(_("slug"), max_length=30, unique=true, editable=false)      class meta:          verbose_name = _("startup type")         verbose_name_plural = _("startup types")         translate = (             "name",         )         ordering = [             "name_" + language_code,         ] 

according documentation can define get_ordering() method use dynamic order:

# admin.py django.contrib import admin .models import startuptype  class startuptypeadmin(admin.modeladmin):     def get_ordering(self, request):         language_code = request.language_code[:2]         return ('name_%s' % language_code,)  admin.site.register(startuptype, startuptypeadmin) 

this works using get_language() instead of request.language_code.

if still not correct language, make sure added locale middleware after session middleware in settings.py, e.g.:

middleware_classes = (      'django.middleware.common.commonmiddleware',      'django.contrib.sessions.middleware.sessionmiddleware',      'django.middleware.locale.localemiddleware',      'django.middleware.csrf.csrfviewmiddleware',      'django.contrib.auth.middleware.authenticationmiddleware',      'django.contrib.messages.middleware.messagemiddleware',      'django.middleware.clickjacking.xframeoptionsmiddleware',  )  

edit: added alternative define dynamic ordering on model.

if want set ordering on model class, seems tad bit more complicated, best come with:

from django.db import models django.db.models.options import options django.utils.translation import get_language django.utils.translation import ugettext_lazy _  transmeta import transmeta, get_real_fieldname   class translatedoptions(options):     @property     def ordering(self):         lang = get_language()[:2]         return [get_real_fieldname(f, lang) f in self.orig_ordering]      @ordering.setter     def ordering(self, value):         self.orig_ordering = value   class transmetaordering(transmeta):     def __new__(cls, name, bases, attrs):         new_class = super(transmetaordering, cls).__new__(cls, name, bases, attrs)         options = translatedoptions(none)         k, v in new_class._meta.__dict__.iteritems():             setattr(options, k, v)         new_class._meta = options         return new_class   class startuptype(models.model):     __metaclass__ = transmetaordering      name = models.charfield(_("name"), max_length=30)     slug = models.slugfield(_("slug"), max_length=30, editable=false)      class meta:         verbose_name = _("startup type")         verbose_name_plural = _("startup types")         translate = (             "name",         )         ordering = (             "name",         ) 

this replace startuptype._meta (an object of type django.db.models.options.options) new object of type translatedoptions expose ordering dynamic property.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -