On 24 jan, 16:43, Filip Gruszczyński <grusz...@gmail.com> wrote: > I would like to achieve something like this: > > class MyModel(models.Model): > .... > > CONST = MyModel.objects.get(id=1) > > The problem is, that during class definition <module>_mymodel table > might not exist or there might not be those objects there.
FWIW, by that time, the MyModel class doesn't even exists yet - you can't refer to the current class object inside a 'class' statement body. > Of course I could do it like this: > > class MyModel(models.Model): > > @classmethod > def CONST1(Class): > return Class.objects.get(id=1) > > but I don't like it, because I would have to call a constant, rather > than just access it and it is against our coding conventions. You could write a custom descriptor then. But the whole idea of a class-level pseudo-constant pointing to a model instance still looks rather backward to me. Oh, and while were at it: the Python standard for the current class param name in a classmethod is 'cls', not 'Class' ;) I though > about having a metaclass, that would add __getattr__ to MyModel, that > would be used during run time, but Model subclasses can't have own > metaclass. Yes they do - as long as their metaclass derives from the appropriate metaclass (ModelBase IIRC but better to check it out). -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.