1

I have this model:

class PetAttribute(models.Model):
    species = models.ForeignKey(PetSpecies, related_name = "pets")
    pet_category = models.ForeignKey(PetCategory, related_name = "pets", blank=True, null=True)
    pet_type = models.ForeignKey(PetType, related_name = "pets", blank=True, null=True)
    additional_fields= models.ManyToManyField( AdditionalField, null= True, blank=True )

Now i want to add an additional option in select (pet_category, pet_type), which is

("0","Other")

in queryset of these. I have tried but form give me an error

Error: Select a valid choice. That choice is not one of the available choices.

here is a one solution of it, but i want to do this by ModelChoiceField

Any suggestion?

Thanks :)

Community
  • 1
  • 1
Ahsan
  • 11,516
  • 12
  • 52
  • 79

1 Answers1

0

While it is possible, are you sure this is what you want to do? You are telling a field that validates choices to the model objects to accept a non-valid answer.

Creating an "other" PetType and PetCategory object or using empty_label as "other" might make more sense than forcing the ModelChoiceField to accept arbitrary values.

Then to find objects with "other" selected, query for None,

pattrs_w_other_pet_type = PetAttribute.object.filter(pet_type=None)
dting
  • 38,604
  • 10
  • 95
  • 114
  • 1
    thanks for replying .... Sir is there any way, i can override the clean method and to allow this "Other" option. basically when user select other option from choices then i want to fill him a text field, which will dynamically shown to user, after selecting this "other" option this text field will be mandatory to fill... so please help me in this case.. – Ahsan May 03 '11 at 06:15