Django:在Abstract模型中定义泛型管理器

时间:2017-03-01 09:22:52

标签: django django-models

使用以下代码并为每个类定义合适的NATURAL_KEY失败(而不是在每个类中定义不同的管理器,复制相同的代码):

class NexchangeManager(models.Manager):
    def get_by_natural_key(self, param):
        if param == "*":
            return self.all()
        lookup = {self.NATURAL_KEY: param}
        return self.get(**lookup)


class NexchangeModel(models.Model):
    class Meta:
        abstract = True
    objects = NexchangeManager()

Djagno抱怨字段复制,尽管NexhcnageModel是一个抽象模型。 我应该使用mixin吗?

错误:

django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/beoleg/dev/nexchange/core/fixtures/pairs_cross.json': 'NexchangeManager' object has no attribute 'NATURAL_KEY': (core.pair:pk=1) field_value was '['LTC']'

乍一看有点过于复杂的代码的目的是,在我的灯具中有这样的东西:

[
  {
    "model": "payments.paymentpreference",
    "pk": 8,
    "fields": {
      "user": ["onit"],
      "identifier": "paypal@nexchange.co.uk",
      "payment_method": 12,
      "comment": "Please send the funds as a personal payment (this is a precaution to prevent charge backs, payments for goods and services will be automatically declined)",
      "currency": [
          ["*"]
      ],
      "created_on":"2016-11-01T17:41:28+00:00",
      "modified_on":"2016-11-01T17:41:28+00:00"
    }
  }
]

Instaed of:

[
  {
    "model": "payments.paymentpreference",
    "pk": 8,
    "fields": {
      "user": ["onit"],
      "identifier": "paypal@nexchange.co.uk",
      "payment_method": 12,
      "comment": "Please send the funds as a personal payment (this is a precaution to prevent charge backs, payments for goods and services will be automatically declined)",
      "currency": [
         ["USD"],
         ["RUB"],
         ["EUR"],
         ["GBP"],
         ["JPY"],
         ["HRK"],
         ["CHF"],
         ["PLN"],
         ["RON"],
         ["BGN"],
         ["CZK"],
         ["AUD"],
         ["CAD"],
         ["NOK"],
         ["SEK"],
         ["DKK"],
         ["HUF"],
         ["TRY"],
         ["ZAR"],
         ["NZD"],
         ["BRL"],
         ["IDR"],
         ["ILS"],
         ["INR"],
         ["KRW"],
         ["MXN"],
         ["MYR"],
         ["PHP"],
         ["THB"]
      ],
      "created_on":"2016-11-01T17:41:28+00:00",
      "modified_on":"2016-11-01T17:41:28+00:00"
    }
  }
]

2 个答案:

答案 0 :(得分:1)

我不明白你写经理的方式。如果NATURAL_KEY是每个模型的属性,我会写:

class NexchangeManager(models.Manager):
    def get_by_natural_key(self, param):
        qs = self.get_queryset()
        if param == "*":
           return qs.all()
        lookup = {qs.model.NATURAL_KEY: param}
        return qs.filter(**lookup)

答案 1 :(得分:0)

注意:这个答案是基于albars的答案,但是因为允许使用pk params而得到了改进。

管理器:

class NexchangeManager(models.Manager):
    def get_by_natural_key(self, param):
        qs = self.get_queryset()
        if param == "*":
            return self.all()
        lookup = {qs.model.NATURAL_KEY: param}
        return self.get(**lookup)

通用Model类:

class NexchangeModel(models.Model):
    class Meta:
        abstract = True
    objects = NexchangeManager()