动态添加字段到 django 模型
我有一个 Pet 模型,看起来
class Pet(models.Model):
STATUS_CHOICES=(
(1,'Listed for sale'),
(2,'Dead'),
(3,'Sold'),
)
name = models.CharField(_("name"), max_length=50 )
species = models.ForeignKey(PetSpecies, related_name = "pets")
pet_category = models.ForeignKey(PetCategory, related_name = "pets")
pet_type = models.ForeignKey(PetType, related_name = "pets")
# want to add dynamic fields here depends on above select options(species, category, type)
color = models.CharField(_("color"), max_length=50, null=True, blank=True)
weight = models.CharField(_("weight"), max_length=50, null=True, blank=True)
我已经阅读了 动态模型,这会有帮助吗为我?或者我应该做点别的什么?如果有人知道请用一段代码指导我。
谢谢 :)
I have a model of Pet, which looks like
class Pet(models.Model):
STATUS_CHOICES=(
(1,'Listed for sale'),
(2,'Dead'),
(3,'Sold'),
)
name = models.CharField(_("name"), max_length=50 )
species = models.ForeignKey(PetSpecies, related_name = "pets")
pet_category = models.ForeignKey(PetCategory, related_name = "pets")
pet_type = models.ForeignKey(PetType, related_name = "pets")
# want to add dynamic fields here depends on above select options(species, category, type)
color = models.CharField(_("color"), max_length=50, null=True, blank=True)
weight = models.CharField(_("weight"), max_length=50, null=True, blank=True)
i have read the Dynamic Models, would it be helpful for me? or should i do something else? if anyone know please guide me with piece of code.
thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您共享的链接并不是您所需要的...
您需要的是一个数据库表结构,可以存储不同的类型定义以及与其相关的记录...在这一点上,您可能会需要更改您的数据库表结构...
首先您可以定义一个表来存储类别标签,例如
PetTyper 是宠物类型的基本记录表,因此您将在此表中定义每个宠物,附加字段将显示哪些额外字段将显示在每条记录上。不要忘记这些表将记录基本类型和附加结构,而不是添加的动物记录。
因此,此类记录可能包含以下信息:
pettYpe:哺乳动物,狗,拉布拉多猎犬,additional_info = [颜色,重量]
这告诉您任何记录为拉布拉多猎犬的狗都会有颜色和体重信息...
对于记录到数据库的每只拉布拉多猎犬都会将数据记录到这些表中:
因此,当您创建新的宠物条目时,您将定义一个petTyper 并将每个附加字段数据的名称添加到AdditionalFields。在新的宠物记录表单中,您将首先获取宠物类型,然后从AdditionalFields 表中获取每个附加信息数据。用户在选择类型后输入宠物名称,然后添加颜色和重量信息(来自上面的示例)。您将从表单中获取这些信息,并在 pet 表上创建一条记录,并将有关该记录的每个具体信息添加到 petSpecifications 表中...
这种方式非常困难,您不能使用一些基本的 django 功能,例如表单表单模型等因为您从PetTyper 和AdditionalFields 表中读取数据并通过这些信息创建您的表单。并将发布的信息记录到 Pet 和 petSpecifications 表中...
Actually,the link you shared is not what you need...
What you need is a database table structure that can store diffrent types definitions and records related to them... In that point, you probably will need to change your database table structure...
First you may define a table that will store category labels like
PetTyper is a basic record table for Pet types, So you will define each pet in this table, additional field will show which extra fields will be shown on each record. Do not forget that these tables will record basic type and additional struvcture, not records of animals added..
So a such record may contains informations like:
pettYpe: mammal, dog, Labrador Retriever , additional_info = [color, weight]
Which tells you that any dog recorded as LAbrador Retreiver will have color and weight information...
For each Labrador Retreiver recorded to the database will record data into these tables:
So when yo create a new pet entry, you will define a petTyper and add names of each additional field data to AdditionalFields. In your new pet record form, you will get the pet typer first, then get each additonal info data from AdditionalFields table. User will enter a pets name after he/she chooses type, then add color and weight info (from the exapmle above). You will get those information from the form and create a record on the pet table and add each specific info about that record to the petSpecifications table...
That way is quite hard and you can not use some basic django features lke forms form models etc. Because you read data from PetTyper and AdditionalFields table and crrete your form through those information. And record the posted information to Pet and petSpecifications table...