Should I override/overload the init function in my ApprovalForm class? Am I misusing the ModelForm constructor?
On Jan 19, 2:27 pm, "tom.s.macken...@gmail.com" <tom.s.macken...@gmail.com> wrote: > I want to create a form based on a model and inject data into it > manually. See below for implementation. > > class Approval(models.Model): > company = models.ForeignKey(Company) > product = models.ForeignKey(Product) > approval_type = models.ForeignKey(Approval_Type, help_text='How > did you get your information?') > ingredient = models.ForeignKey(Ingredient,default=1) > pub_date = models.DateTimeField > (default=datetime.datetime.now) > approved = models.BooleanField() > url = models.CharField(null=True, blank=True, > max_length=100) > comment = models.CharField(null=True, blank=True, > max_length=200) > user = models.ForeignKey(User) > show = models.BooleanField(default=False) > show_pub_date = models.DateTimeField(null=True, blank=True) > > class ApprovalForm(ModelForm): > class Meta: > model = Approval > fields = > ('company','product','approval_type','approved','url','comment') > > #Views.py > > products = Product.objects.filter(company=company_id) > companies = Company.objects.filter(id=company_id) > types = Approval_Type.objects.all() > > data = {'company': companies, > 'product': products, > 'approval_type': types, > 'approved': False, > 'url': 'www.someWebSite.com', > 'comment': 'Enter Comment Here',} > > form = ApprovalForm(data, initial={'company': company_id, > 'product':product_id}) > > My Expectations for the company and product content is: > 1. I want all the companies injected into the form. > 2. I only want the products that relate to the initial company that is > selected. (This is because in the template I want to list only the > products that relate to the current selected company via JQuery). > > The only fields being assigned are the ones that are not multiple > choice ones (url is ok, comment, etc). How do I assign the data for > the company and product fields? > > Any advise is welcome --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---