dynamically add fields to django models
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_cate开发者_JAVA技巧gory = 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 :)
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...
精彩评论