动态添加字段到 django 模型

发布于 2024-11-02 04:10:56 字数 804 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-11-09 04:10:56

实际上,您共享的链接并不是您所需要的...

您需要的是一个数据库表结构,可以存储不同的类型定义以及与其相关的记录...在这一点上,您可能会需要更改您的数据库表结构...

首先您可以定义一个表来存储类别标签,例如

class PetTyper(models.Model):
    specy = models.ForeignKey(...)
    category = models.ForeignKey(...)
    type = models.Foreignkey(...)
    ...
    additional_fields= models.ManyToManyField(AdditionalFields)

class AdditionalFields(Models.Model):
    label = models.CharField(_("Field Label")

PetTyper 是宠物类型的基本记录表,因此您将在此表中定义每个宠物,附加字段将显示哪些额外字段将显示在每条记录上。不要忘记这些表将记录基本类型和附加结构,而不是添加的动物记录。

因此,此类记录可能包含以下信息:

pettYpe:哺乳动物,狗,拉布拉多猎犬,additional_info = [颜色,重量]

这告诉您任何记录为拉布拉多猎犬的狗都会有颜色和体重信息...

对于记录到数据库的每只拉布拉多猎犬都会将数据记录到这些表中:

class Pet(models.Model):
    name = models.CharField(...)
    typer = models.ForeignKey(PetTyper) # this will hold records of type, so no need for specy, category and type info in this table
    ... # and other related fields

class petSpecifications(models.Model):
    pet = Models.ForeignKey(Pet) # that data belongs to which pet record
    extra_data_type = Models.ForeignKey(AdditionalFields) # get the label of the extra data field name
    value = models.CharField(...) # what is that extra info value

因此,当您创建新的宠物条目时,您将定义一个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

class PetTyper(models.Model):
    specy = models.ForeignKey(...)
    category = models.ForeignKey(...)
    type = models.Foreignkey(...)
    ...
    additional_fields= models.ManyToManyField(AdditionalFields)

class AdditionalFields(Models.Model):
    label = models.CharField(_("Field Label")

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:

class Pet(models.Model):
    name = models.CharField(...)
    typer = models.ForeignKey(PetTyper) # this will hold records of type, so no need for specy, category and type info in this table
    ... # and other related fields

class petSpecifications(models.Model):
    pet = Models.ForeignKey(Pet) # that data belongs to which pet record
    extra_data_type = Models.ForeignKey(AdditionalFields) # get the label of the extra data field name
    value = models.CharField(...) # what is that extra info value

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文