限定通用管理用下面的代码和限定每个每类合适NATURAL_KEY
失败(而不是定义在每个类不同的经理,其复制相同的代码):Django的:在一个抽象模型
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": "[email protected]",
"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的:
[
{
"model": "payments.paymentpreference",
"pk": 8,
"fields": {
"user": ["onit"],
"identifier": "[email protected]",
"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"
}
}
]
失败怎么办?显示实际的错误。并举例说明您如何以及在哪里定义NATURAL_KEY。 –
@DanielRoseman编辑!谢谢! –