Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Level Property for Django model in Python

Tags:

python

django

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 :).

like image 797
Ludo Avatar asked Nov 04 '22 19:11

Ludo


1 Answers

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

like image 177
DrTyrsa Avatar answered Nov 12 '22 10:11

DrTyrsa