我需要访问decorator中的模型类文件:
from django.db import models
class Register:
REGISTRY = {}
def __init__(self, name):
self.name = name
def __call__(self, cls):
self.register(cls)
return cls
def register(self, cls):
print(cls.__name__, '-', self.name) # prints: Item - Item registered
print(cls._meta.get_fields()) # raises: django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
@Register('Item registered')
class Item(models.Model):
title = models.CharField(max_length=200, verbose_name='Title')
cls._meta.get_fields()引发:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
如何访问字段?
答案 0 :(得分:0)
所以,我尝试使用django 1.8在我自己的机器上复制它,我发现._meta.fields
也可以访问模型字段。
class Register:
REGISTRY = {}
def __init__(self, name):
self.name = name
def __call__(self, cls):
self.register(cls)
return cls
def register(self, cls):
print(cls.__name__, '-', self.name) # prints: Item - Item registered
print(cls._meta.fields) # prints all fields