在Django模型中继承元类

时间:2016-12-31 14:45:21

标签: python django

我有一个问题作为新手,我正在寻找与Django类中继承Meta类相关的概念。以下是模型,如果有人可以帮助我,我将不胜感激。

data$Mean_Aspect <- 0
library(circular)
angles = c(200.072,204.037,198.591,193.151,192.779,187.503,198.549,196.675,199.618,191.083,187.242)
anglecir =  circular(angles, type="angles", units="degrees",modulo="2pi", template='geographics')
mean(anglecir)

回溯:

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")

我已经改变了类导入行为以便完成以下操作。这可能导致了这个问题。原因是我需要简化商店仪表板应用程序的代码。所以我导入了默认的Django源来导入Django类,并改变它如下:

    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'

Filename是loading.py,我是从Django-Oscar学到的。然而,我做了一些小改动,以满足我的需要。所以问题可能出在这里。请协助

1 个答案:

答案 0 :(得分:20)

您应该声明您的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