简体   繁体   English

Django:调用元类库时出错

[英]Django: Error when calling the metaclass bases

Here is the error 这是错误

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 TypeError:调用元类基类元类冲突时出错:派生类的元类必须是其所有基类的元类的(非严格)子类

The class in question within my models.py 我的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)

The line I am getting the error on: 我收到错误的行:

name = models.CharField(max_length=128)

But I (think) it means this one: 但我(想)这意味着这个:

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

I'm not sure what it means exactly, how can I inherit my models from models.Model and forms.Form within the same class? 我不确定它究竟是什么意思,我如何从models.Model和forms.Form继承我的模型在同一个类中? Can I not pass two values when creating my class? 在创建课程时,我不能传递两个值吗? If so how? 如果是这样的话?

ANOTHER EDIT 另一个编辑

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

Full traceback: 完全追溯:

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

This is the problem: 这就是问题:

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

You're trying to inherit from Model and Form. 您正尝试从模型和表单继承。 You can't, and you shouldn't. 你不能,你不应该。

You can't because the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases. 您不能,因为派生类的元类必须是其所有基础的元类的(非严格)子类。 Form has a metaclass: 表格有一个元类:

__metaclass__ = DeclarativeFieldsMetaclass

Model also has a metaclass: 模型还有一个元类:

__metaclass__ = ModelBase

If you were to do this, you would need to set a metaclass which derives from both of those. 如果你这样做,你需要设置一个从这两个派生的元类。

However, you shouldn't do this, because django has ModelForms, which exist to create forms that model models, saving you the trouble of the complexity here. 但是,你不应该这样做,因为django有ModelForms,它可以创建模型模型,为你节省复杂的麻烦。 Just stop inheriting from Form. 只需停止继承Form。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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