Django:调用元类基时出错

2024-09-22 14:38:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有个错误

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

我的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

Tags: djangoinpyhomemodelslibpackagesline
1条回答
网友
1楼 · 发布于 2024-09-22 14:38:08

这就是问题所在:

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

你试图从模型和形式中继承。你不能,也不应该

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

__metaclass__ = DeclarativeFieldsMetaclass

模型还有一个元类:

__metaclass__ = ModelBase

如果要这样做,则需要设置一个从这两个类派生的元类。

但是,您不应该这样做,因为django有model forms,可以创建用于建模的表单,省去了这里复杂性的麻烦。停止从表单继承。

相关问题 更多 >