2017-08-09 38 views
0

在開發Oscar Commerce項目的發貨方法時,我發現我需要兩個屬性;一個包含表示產品重量的浮點值(我使用Scale class來稱量產品)以及一個鏈接到貨運集裝箱模型的實體屬性。在Django-Oscar中編程定義ProductAttributes的位置

屬性需要分配給任何需要裝運的產品類別。我在哪裏/如何創建它們?我有以下代碼,但我不確定它適合哪裏。

from oscar.core.loading import get_model 
ProductAttribute = get_model('catalogue', 'ProductAttribute') 

ProductAttribute.objects.get_or_create(code='weight', 
     product_class=[product class], 
     defaults={ 
      'name': 'Weight', 
      'type': ProductAttribute.FLOAT, 
      }) 
ProductAttribute.objects.get_or_create(code='box', 
     product_class=[product class], 
     defaults={ 
      'name': 'Box used for shipping' 
      'type': ProductAttribute.ENTITY, 
     }) 

哪裏是添加此代碼的最佳地方?

回答

1

標準的Django方法是使用data migration

如果您不希望數據自動加載,那麼替代方案將使用fixture

我們通常只是通過管理員設置屬性。此外PR #2448旨在添加儀表板支持添加屬性。