Django:调用元类的时候出错 [英] Django: Error when calling the metaclass bases

查看:643
本文介绍了Django:调用元类的时候出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误


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)
#记录信息
slug = models .SlugField()
date_added = models.DateField(auto_now_add = True)
time_added = models.TimeField()
added_by_user = models.CharField(max_length = 64)
last_edi t_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):

我不知道这是什么意思,我怎么能从model.Model和forms.Form在同一个类中继承我的模型?创建我的课时,我不能传递两个值吗?如果是这样吗?



其他编辑

 我所有的进口
from django.db import models
from django import forms
from django.contrib.localflavor import generic
from django.contrib.localflavor.gb.forms import GBPostcodeField,GBCountySelect

完整追溯:

 追溯(最近的最后一次调用):
文件manage.py,第10行,< module>
execute_from_command_line(sys.argv)
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/__init__.py,第443行,在execute_from_command_line
utility.execute()
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/management/__init__.py,第382行,执行
self.fetch_command(子命令).run_from_argv(self.argv)
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/core/管理/ base.py,第196行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7 /site-packages/django/core/management/base.py,第231行,执行
self.validate()
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2。 7 / site-packages / django / core / management / base.py,第266行验证
num_errors = get_validation_errors(s,app)
文件/home/jws1000/.virtualenvs / greutenfree/lib/python2.7/site-packages/django/core/management/validation.py,第30行,get_validation_errors
for(app_name,error)in get_app_errors()。items():
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py,第158行,get_app_errors
self._populate()
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py,第64行,_populate
self.load_app(app_name ,True)
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/loading.py,第88行,在load_app
模型中= import_module('。models',app_name)
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/utils/importlib.py,第35行,import_module
__import __(name)
文件/home/jws1000/envs/glutenfree/glutenfree/glutenfree/listings/models.py,第9行在< module>
class Business(models.Model,forms.Form):
文件/home/jws1000/.virtualenvs/glutenfree/lib/python2.7/site-packages/django/db/models/base。 py,第41行,__new__
new_class = super_new(cls,name,bases,{'__module__':module})
TypeError:调用元类时出错
metaclass conflict:派生类的元类必须是所有基类的元类的(非严格)子类


解决方案

这是问题:

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

您正在尝试从模型和表单继承。你不能,而不应该。



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

  __ metaclass__ = DeclarativeFieldsMetaclass 

模型也有一个元类:

  __ metaclass__ = ModelBase 

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

$ b但是,您不应该这样做,因为django具有ModelForms,它存在用于创建模型模型的表单,从而节省了复杂性的麻烦。只需停止从Form继承。


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

The class in question within my 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? 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. Just stop inheriting from Form.

这篇关于Django:调用元类的时候出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆