Thanks for the information, I will look into your suggestions. The getLanguage design did seem to work though, as changing the language did have an effect on the __unicode__ function: in the admin interface the correct values (for that language) were shown. At least it did so on the local runserver environment. I can see though that even though __unicode__ is called every time, the ordering is probably called only at import time, and my little plan won't work.
Have to find a solution for that problem first I guess, before I even start to tackle the problem of how to set the ordering... On Dec 18, 5:00 pm, Bill Freeman <ke1g...@gmail.com> wrote: > I can't promise that it will do what you want, but your problem is > that at the time > you try to set the value of Meta.ordering, class Substrate does not yet exist. > You can dig into the Substrate.objects._meta later to add ordering, probably. > > Your design wouldn't have worked anyway, since getLanguage would only > have been called once, at import time, and even if that worked, it wouldn't > follow current language as it changes (I presume) from request to request. > > Oh, and since you're not calling getLanguage on a class instance, it would > have to be declared a staticmethod or classmethod, and not get the self > argument or replace it with cls (for classmethod). Static method would be > the choice here (and no self) > > It's mildly possible that ordering is allowed to be a callable. If > so, you could > assign it to a function (not a method of the not yet defined Substrate). > That's > the function itself, no parentheses. The function would return the list. > > If ordering can't be a callable, you could create a class that implements > enough > of list behavior to satisfy the framework (a subject for research or > experimentation, > but iteration might be enough) and set ordering to an instance, whose apparent > content depends on the current language. > > It still might not work, if the __metaclass__ processes Meta.ordering at clas > definition time. You have the source code. > > Bill > > On Fri, Dec 18, 2009 at 6:07 AM, Kevin Renskers <i...@bolhoed.net> wrote: > > Hi, > > > I am having a bit of a problem. I have a model with 3 name fields, > > each for a different language. By creating an __unicode__ function > > like below, I always get the correct name for the current language. > > Works fine. > > > But, I want to change the default ordering for this model, so that in > > each language, the correct order is used. Sadly, this doesn't work, as > > I get the error "NameError: name 'Substrate' is not defined". Also > > "self" and "super" don't seem to work. I just can't seem to be able to > > access the parent class from within the Meta class. > > > I can't even repeat the getLanguage function into the Meta class, > > because then I get an the error "TypeError: 'class Meta' got invalid > > attribute(s): getLanguage". > > > class Substrate(models.Model): > > name_en = models.CharField(max_length=255) > > name_de = models.CharField(max_length=255, blank=True) > > name_nl = models.CharField(max_length=255, blank=True) > > > def getLanguage(self): > > from django.utils import translation > > current_language = translation.get_language() > > if not current_language: > > current_language = 'en' > > return current_language > > > def getName(self): > > current_language = self.getLanguage() > > name = getattr(self, 'name_'+current_language, self.name_en) > > if not name: > > name = self.name_en > > > return name > > > def __unicode__(self): > > return self.getName() > > > class Meta: > > ordering = [('name_%s' % Substrate.getLanguage()), 'name_en'] > > > If anyone has an idea how on to do this, I would be very happy. > > Thanks! > > > Cheers, > > Kevin > > > -- > > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.