Re: populating forms using models
To populate select field you have many options. First is using ModelChoiceField. The ModelChoiceField have an argument named queryset, you can use like this example provincia = forms.ModelChoiceField(queryset=Provincia.objects.all() ..) If you can't have a FK of Provincia table you can use a Choice Field and populate externaly like this provincia = forms.ChoiceField( ... ) and inside the form declare a new method named populate(self, list_of_provincias): (for example) and inside this method you can do this... self.fields['provincia'].choices = list_of_provincias In other situations you can populate a ModelcoiceField externaly too: example: You need to populate a modelchoice field with a queryset depending in the user loged ... def populate(self,user): self.fields['boards'].queryset = Board.objects.filter( guild = Guild.objects.get(owner=user) ) When you create a form yo need to call the .populate method with request.user as argument. 2008/11/1 Rock <[EMAIL PROTECTED]> > > In brief, your forms field for province does not have to have anything > to do with your corresponding model field. > Just make a forms.ChoiceField with the data filled in that you need > (or else look up the pattern for loading dynamic data into a > forms.ChoiceField and do something like that.) If you go the dynamic > route, make sure your form gets initialized properly when you create > it for initial use and also when you create it for POST processing. > > > > > -- neo2001[at]gmail.com neo[at]art-xtreme.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: populating forms using models
In brief, your forms field for province does not have to have anything to do with your corresponding model field. Just make a forms.ChoiceField with the data filled in that you need (or else look up the pattern for loading dynamic data into a forms.ChoiceField and do something like that.) If you go the dynamic route, make sure your form gets initialized properly when you create it for initial use and also when you create it for POST processing. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
populating forms using models
Hi I'm working on a registration form which is based, among the others, on this model: class City(models.Model): prov_name = models.CharField(verbose_name='Provincia',max_length=50) (obviously i'm writing down just the field i'm interested in to save some space..) In the form i have to put a select field for the cities (form.forms.ModelChoiceField with a queryset on City) and also a select field for prov_name, in order to populate the list of choices.. Surely a different model (Provinces, and then a foreignkey on City prov_name) would be easier to manage but i can't alter the schema of the database i'm using, so i have to find workarounds.. And i can't do a modelChoiceField based on prov_name because there's no primary key, it's just a field.. Do someone have an idea on how to handle this? Thank you a lot! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---