いくつかの追加フィールドのために auth.models グループを拡張しようとしていました。myapp.models.py で行ったことは次のとおりです。
class ProfileGroupBase(type):
def __new__(cls, name, bases, attrs):
module = attrs.pop('__module__')
parents = [b for b in bases if isinstance(b, ProfileGroupBase)]
if parents:
fields = []
for obj_name, obj in attrs.items():
if isinstance(obj, models.Field): fields.append(obj_name)
Group.add_to_class(obj_name, obj)
return super(ProfileGroupBase, cls).__new__(cls, name, bases, attrs)
class ProfileGroup(object):
__metaclass__ = ProfileGroupBase
class MyGroup(ProfileGroup):
mobile = models.CharField(max_length = 15)
email = models.CharField(max_length = 15)
これらのコードを使用すると、モバイル フィールドとメール フィールドが管理ページのグループに追加されます。ここで、別の manytomany フィールドを追加したいので、これをクラス MyGroup(ProfileGroup) の下に追加しました。
annotations = models.ManyToManyField(Annotation, verbose_name=_('annotation'), blank=True)
次に、同じ models.py ファイルの下に新しいモデルの注釈を作成しました。
class Annotation(models.Model):
index = models.IntegerField()
name = models.CharField(max_length = 100)
ただし、エラーが発生します: name 'Annotation' is not defined. クラス MyGroup とクラス Annotation が同じ models.py の下で定義されていても、実際には分離されているのではないかと思います。私はとても混乱しています、誰もここで何が起こっているのか知っていますか? ありがとう。