【问题标题】:How can i change the field type in django models by overriding the class如何通过覆盖类来更改 django 模型中的字段类型
【发布时间】:2014-10-01 12:14:15
【问题描述】:

我在核心文件夹中有一个类,如下所示:

class AbstractLine(models.Model):
    basket = models.ForeignKey('basket.Basket', related_name='lines',
                           verbose_name=_("Basket"))

    quantity = models.PositiveIntegerField(_('Quantity'), default=1)

现在我想重写这个类,以便将数量 PositiveIntegerField 更改为 DecimalField,如下所示:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from oscar.apps.basket.abstract_models import AbstractLine as ModelLine

class Line(ModelLine):
    quantity = models.DecimalField(_('Quantity'), default=0.25, decimal_places=2, max_digits=3)

我怎样才能做到这一点?

【问题讨论】:

  • AbstractLine实际上是一个抽象类吗?
  • 是的。它是抽象类。
  • 你写的方式有什么问题?
  • 这种方式行不通#Burhan Khalid

标签: django python-2.7 django-oscar


【解决方案1】:

实际上,这是不可能的。看到这个问题:In Django - Model Inheritance - Does it allow you to override a parent model's attribute?

这同样适用于抽象基类,参见this discussion on the Django user's group

最好的办法是简单地从基类中删除该字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2011-12-14
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多