2017-03-01 79 views
0

限定通用管理用下面的代碼和限定每個每類合適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" 
    } 
    } 
] 
+0

失敗怎麼辦?顯示實際的錯誤。並舉例說明您如何以及在哪裏定義NATURAL_KEY。 –

+0

@DanielRoseman編輯!謝謝! –

回答

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) 
+0

django.core.serializers.base.DeserializationError:安裝fixture的問題'/Users/beoleg/dev/nexchange/core/fixtures/pairs_cross.json':'QuerySet'對象沒有屬性'id':(core.pair:pk = 1)field_value是'['BTC']' –

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