Django:调用元类库时出错

Posted

技术标签:

【中文标题】Django:调用元类库时出错【英文标题】:Django: Error when calling the metaclass bases 【发布时间】:2012-07-24 08:17:01 【问题描述】:

这是错误

TypeError:调用元类库时出错 元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类

models.py 中的相关类

class Business(models.Model, forms.Form):
    name = models.CharField(max_length=128)
    tel_no = models.CharField(max_length=11)
    address_ln1 = models.CharField(max_length=128)
    address_ln2 = models.CharField(max_length=128)
    city = models.CharField(max_length=64)
    county = GBCountySelect()
    postcode = GBPostcodeField()
    website = models.URLField(max_length=128)
# Logging Info
    slug = models.SlugField()
    date_added = models.DateField(auto_now_add=True)
    time_added = models.TimeField()
    added_by_user = models.CharField(max_length=64)
    last_edit_time = models.TimeField(auto_now=True)
    last_edit_date = models.DateField(auto_now=True)

我收到错误的那一行:

name = models.CharField(max_length=128)

但我(认为)是指这个:

class Business(models.Model, forms.Form):

我不确定它的确切含义,我如何在同一个类中从 models.Model 和 forms.Form 继承我的模型?创建课程时我不能传递两个值吗?如果有怎么办?

另一个编辑

All my imports
from django.db import models
from django import forms
from django.contrib.localflavor import generic
from django.contrib.localflavor.gb.forms import GBPostcodeField, GBCountySelect

完整的追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name, True)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/jws1000/envs/glutenfree/glutenfree/glutenfree/listings/models.py", line 9, in <module>
    class Business(models.Model, forms.Form):
  File "/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/base.py", line 41, in __new__
    new_class = super_new(cls, name, bases, '__module__': module)
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

【问题讨论】:

您为什么要这样做?如果您想创建表单,请为此创建一个单独的类。 因为县和邮政编码与业务相关联。将它们放在同一张桌子上不是明智的吗,因为我将逐条访问每条记录? 请完整显示错误。 sscce.org 我已经编辑了我的原始帖子。任何线索将不胜感激,我认为这可能与我的进口中的冲突有关? @jdx 我认为您需要了解 Django 表单是什么。它们与表格无关。 【参考方案1】:

这就是问题所在:

class Business(models.Model, forms.Form):

您正在尝试从 Model 和 Form 继承。你不能,也不应该。

你不能因为派生类的元类必须是其所有基类的元类的(非严格)子类。表单有一个元类:

__metaclass__ = DeclarativeFieldsMetaclass

模型也有一个元类:

__metaclass__ = ModelBase

如果您要这样做,您需要设置一个派生自这两者的元类。

但是,您不应该这样做,因为 django 有 ModelForms,它的存在是为了创建模型模型的表单,从而为您省去了这里复杂性的麻烦。停止从 Form 继承即可。

【讨论】:

我删除了 forms.Form,并使用本地风味的东西保留了我的“表单”,它似乎有效。因为我没有从 forms.* 调用中得到任何东西。感谢您的帮助。 @jdx 不客气。请接受这个答案并投票。

以上是关于Django:调用元类库时出错的主要内容,如果未能解决你的问题,请参考以下文章

子类 Django ModelBase(Django 模型的元类)

django 模型元类的标准文档字符串是啥?

在类似 django 的表单实现中使用元类有啥好处?

有啥方法可以将变量传递给 django 元类?

元类与 Django 中的 modelformset_factory 冲突

如何做类似 Django 模型的元类技巧