2
我有一個問題,我一次又一次地使用相同的代碼(保存項目,而不是該項目的項目事務)。模型管理器中的Django動態查詢
所以我想也許我需要一個模型管理器,它會幫助我,而且我在Django中不是很有經驗,所以我想知道你是如何處理這類問題的。
我的想法是創建像的InventoryManager:
class InventoryManager(models.Manager):
def create_item(self, **kwargs):
#
def create_item_trans(self, item, **kwargs):
pass
def create_item_with_trans(self, **kwargs):
# create item
# create item_trans
# return item with item_trans
但在create_item_with_trans,我沒有辦法送 kwargs,導致** create_item acceps不同kwargs和create_item_transaction接受不同的** kwargs
我也在思考這個像發送字典,如: '{「項目」 {...}「item_trans」:{...}}' ,但我不知道這是正確的方式,非常感謝 – Jazzy
你認爲我應該在模型管理器上寫這個,或者我應該創建一個單獨的文件,哪裏有我的幫助函數嗎? – Jazzy
我通常會在應用程序內部創建一個'managers.py',並將'models.Manager'的子類放在那裏。不需要助手文件。 – lucasnadalutti