![](/img/trans.png)
[英]Django signals not sending on save() for models inheriting from a parent class?
[英]Inheriting Meta Class in Django Models
我有一个问题作为新手,我正在寻找与Django类中继承Meta类相关的概念。 以下是模型,如果有人可以帮助我,我将不胜感激。
class Base(models.Model):
code = AutoSlugField(_("Slug"), max_length=128, unique=True,
populate_from='name')
name = models.CharField(_("Name"), max_length=128, unique=True)
description = models.TextField(_("Description"), blank=True)
countries = models.ManyToManyField('Country',
blank=True, verbose_name=_("Countries"))
is_discounted = False
class Meta:
app_label = 'shipping'
ordering = ['name']
verbose_name = _("Shipping Method")
verbose_name_plural = _("Shipping Methods")
class OrderAndItemCharges(Base):
price_per_order = models.DecimalField(
_("Price per order"), decimal_places=2, max_digits=12,
default=D('0.00'))
price_per_item = models.DecimalField(
_("Price per item"), decimal_places=2, max_digits=12,
default=D('0.00'))
free_shipping_threshold = models.DecimalField(
_("Free Shipping"), decimal_places=2, max_digits=12, blank=True,
null=True)
class Meta(Base.Meta):
app_label = 'shipping'
verbose_name = _("Order and Item Charge")
verbose_name_plural = _("Order and Item Charges")
追溯:
class OrderAndItemCharges(Base):
File "C:\Users\AliKhan\supermarekt\market\shipping\models.py", line 46, in Or
derAndItemCharges
class Meta(Base.Meta):
AttributeError: type object 'Base' has no attribute 'Meta'
我已经改变了类导入行为,以便完成以下操作。 这可能导致了这个问题。 原因是我需要简化商店仪表板应用程序的代码。 所以我导入了默认的Django源来导入Django类并改变它如下:
from django.apps.config import MODELS_MODULE_NAME
def get_class(module_label, classname):
return get_classes(module_label, [classname])[0]
def get_classes(module_label, classnames):
if not module_label:
raise ValueError(
"Module Does Not Exists")
market_module_label = "%s" % (module_label)
market_module = _import_module(market_module_label, classnames)
return _pluck_classes([market_module], classnames)
def _import_module(module_label, classnames):
try:
return __import__(module_label, fromlist=classnames)
except ImportError:
__, __, exc_traceback = sys.exc_info()
frames = traceback.extract_tb(exc_traceback)
if len(frames) > 1:
raise
文件名是loading.py,我是从Django-Oscar学到的。 然而,我做了一些小改动,以满足我的需要。 所以问题可能出在这里。 请协助
您应该声明您的Base
类抽象,以便将其Meta
类作为属性进行访问。
class A(models.Model):
class Meta:
app_label='yourapp'
abstract=True
class B(A):
class Meta(A.Meta):
db_table='yourtable'
相关文档: https : //docs.djangoproject.com/en/1.10/topics/db/models/#meta-inheritance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.