Ludo
Ludo

Reputation: 2829

Class Level Property for Django model in Python

I have a Django model storing some infrequently changed but often used data. I'm looking for a pattern so that I can refer to them as kind of static class variables, eg, like SomeModel.Bar or SomeModel.Baz.

At the moment I'm using a staticmethod like so:

class SomeModel(models.Model):
    name = models.CharField(max_length=100)

    @staticmethod
    def Baz():
        #retrieve from cache or lookup Baz item
        return baz

Meaning I refer to the items as SomeModel.Baz() but this just doesn't quite feel right. I just get the impression that I'm doing something wrong. I don't want to make it a property, as I don't want an instance to refer to the item.

Can anyone point me at a pattern or example to show me is I can implement a class level property in this way? Or tell me why I should be doing something totally different? Thanks :).

Upvotes: 5

Views: 956

Answers (1)

DrTyrsa
DrTyrsa

Reputation: 31951

If you want some logic for model which doesn't belong to particular row, write custom model manager.

Upvotes: 2

Related Questions