简体   繁体   中英

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

如果您想要一些不属于特定行的模型逻辑,请编写自定义模型管理器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM