1

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 :)

5
  • Why would weight be a CharField? Commented Apr 18, 2011 at 7:32
  • i roughly enter these fields.. want to focus on dynamic fields ... Commented Apr 18, 2011 at 7:39
  • I'm pretty sure dynamic fields aren't what you are looking for. Do you have any examples of what you want to add depending on the category, species, type? Commented Apr 18, 2011 at 7:51
  • yes i have ... species = Mammals, category = Dogs and type = Labrador Retriever, Commented Apr 18, 2011 at 8:15
  • now i want different fields for "Labrador Retriever", or if enter the category = Birds, and then i select parrots, then i want to add a dynamically field "Speaking Parrots" Commented Apr 18, 2011 at 8:21

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

It was a quick answer, so if it look complex and hard-to-understand, i will try to explain it more specifically. Sorry for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.