[英]Given Products, each with multiple Versions, what is the preferred/best way to support custom attributes at the Product/Version level in my Django app
我正在構建我的第一個Django應用程序來管理多個SaaS產品。
這需要為每個產品的每個版本存儲自定義屬性。
例如,發布了新版本的產品,其中包括該產品的早期版本不支持的新配置選項。
我需要能夠跟蹤新版本的每個實例的那些新值。
我想我希望管理員能夠按版本在產品級別添加“自定義字段”。
尋找有關最佳方法的建議。
謝謝。
決定對每個產品使用子類,因為每個子類都有一組有限的特定屬性,這些屬性不會隨時間變化很大或根本不會變化。 感謝您的寶貴意見。 學到了很多 :-)
跟蹤模型版本的常用方法是使用django-reversion 。
聽起來每個實例都需要自己的自定義屬性。 這意味着不需要更改與產品和版本有關的模型。 這很好,因為模型只能隨代碼一起更改(除非您進入動態生成模型的過程,這通常不是一個好主意)。
因此,無論版本如何,您都需要能夠為每個Product實例的屬性建模。 這應該是一個簡單的數據建模練習,不一定與Django有關。
這很容易建模,具體取決於您要如何管理屬性。
class Version(models.Model):
version = models.CharField(max_length=10)
class ProductAttributes(models.Model):
name = models.CharField(max_length=64)
description = models.CharField(max_length=255)
# other fields as necessary
class Product(models.Model):
name = models.CharField(max_length=64)
version = models.ForeignKey(Version)
attributes = models.ManyToManyField(ProductAttributes, related_name='products')
那應該是以最基本的方式對您的建模進行排序。 現在,讓我們創建一些實例。
v1 = Version(version='1.0.0')
v1.save()
hosted = ProductAttributes(name='Hosted', description='We host the apps!')
hosted.save()
shiny = ProductAttributes(name='Shiny', description='I like shiny')
shiny.save()
p = Product(name='Web Based Email', version=v1)
p.save()
p.attributes.add(hosted)
p.attributes.add(shiny)
p.attributes.all()
# shows shiny and hosted!
您可以調整ModelAdmin for Product,以便可以在添加或編輯產品時內聯添加ProductAttributes。 您也可以為ProductAttributes擁有一個單獨的ModelAdmin,以便可以創建一個已知屬性列表,這些列表可以在以后應用於產品。
有兩種基本方法。
使用基於文檔的數據庫(例如,“ NoSQL”),例如Couch或Mongo。 它們具有靈活的模式,因此可以在產品上進行多種更改。
使用實體屬性值( Wikipedia )架構模式。 django-eav是提供此功能的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.