Re: dynamic forms and custom methods
I think jquery may be the right way to go for this. It will allow you to evaluate the weights in real time so you won't have to submit the form before any validation errors would be returned. Working it at the model level may get tricky, for ease I would handle it at the form or view level. On Aug 12, 9:30 am, Steve Holdenwrote: > On 8/12/2010 8:36 AM, lingrlongr wrote: > > > > > Hi Nick, > > > Thanks for the reply. Yes, the department weights will be used in a > > grading scheme. One department may be 25% (0.25), etc... But in the > > end, the weights should total 1.0. For example, in my Store model: > > > class Store(models.Model): > > #...etc... > > > def _get_total_dept_weight(self): > > return self.department_set.all().aggregate(Sum('weight')) > > ['weight_sum'] > > > def save(self, *args, **kwargs): > > if self._get_total_dept_weight() <> Decimal('1.0'): > > # fail > > super(Store, self).save(*args, **kwargs) > > > So in the case of my custom form, I was thinking about raising a > > validation error if the totals submitted in the form don't total 1.0. > > But I was thinking over the weekend, maybe this isn't the best place > > for it after all. Maybe it should stay in the model because why does > > the form care if the weights is 1.0? The model does... Perhaps > > instead, raise a ValidationError exception from the Store's save() > > method? > > Yes, you could do that - and even if you used JavaScript on the client > side to enforce the total it would still be necessary to avoid malicious > clients. > > > What are your thoughts? > > Alternatively, you could just normalize the weights after the user > submits them, or even leave them as they are and normalize them when you > use them. Surely the important thing is the weightings and their > relative values. You can simply divide each weighting by the sum of all > weightings before you apply them. > > regards > Steve > > > > > > > On Aug 11, 3:58 pm, Nick wrote: > >> Are you trying to create a save function that evaluates all the > >> weights and returns an error if they are more than 1? > > >> On Aug 10, 3:17 pm, lingrlongr wrote: > > >>> I 'm trying to create a form dynamically. This works just fine, but > >>> there's no way for the form to offer any customized validation, by way > >>> of the clean() method. > > >>> def get_dept_weight_form(store): > >>> fields = {} > >>> s = Store.objects.get(pk=store.id) > >>> for d in store.department_set.all(): > >>> fields['id_%d' % d.id] = forms.DecimalField( > >>> label = d.name, > >>> initial = d.weight > >>> ) > >>> return type('WeightForm', (forms.BaseForm,), {'base_fields': > >>> fields}) > > >>> Basically, this form spits out a label showing the department name and > >>> the weight for you to enter in a textbox. The weights for all > >>> departments should total 1.0. Should a clean() method handle this? > > >>> I already use an overridden version of save() on the store model, but > >>> that just prints out a warning to stdout. Also, I have this > >>> validation for the admin part by overriding the clean method for my > >>> declared ModelForm. > > >>> Perhaps I should just use the functionality I already created in > >>> models.py and just raise a ValidationError? > > >>> Thx > > -- > DjangoCon US 2010 September 7-9http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: dynamic forms and custom methods
On 8/12/2010 8:36 AM, lingrlongr wrote: > Hi Nick, > > Thanks for the reply. Yes, the department weights will be used in a > grading scheme. One department may be 25% (0.25), etc... But in the > end, the weights should total 1.0. For example, in my Store model: > > class Store(models.Model): > #...etc... > > def _get_total_dept_weight(self): > return self.department_set.all().aggregate(Sum('weight')) > ['weight_sum'] > > def save(self, *args, **kwargs): > if self._get_total_dept_weight() <> Decimal('1.0'): > # fail > super(Store, self).save(*args, **kwargs) > > So in the case of my custom form, I was thinking about raising a > validation error if the totals submitted in the form don't total 1.0. > But I was thinking over the weekend, maybe this isn't the best place > for it after all. Maybe it should stay in the model because why does > the form care if the weights is 1.0? The model does... Perhaps > instead, raise a ValidationError exception from the Store's save() > method? > Yes, you could do that - and even if you used JavaScript on the client side to enforce the total it would still be necessary to avoid malicious clients. > What are your thoughts? > Alternatively, you could just normalize the weights after the user submits them, or even leave them as they are and normalize them when you use them. Surely the important thing is the weightings and their relative values. You can simply divide each weighting by the sum of all weightings before you apply them. regards Steve > On Aug 11, 3:58 pm, Nickwrote: >> Are you trying to create a save function that evaluates all the >> weights and returns an error if they are more than 1? >> >> On Aug 10, 3:17 pm, lingrlongr wrote: >> >>> I 'm trying to create a form dynamically. This works just fine, but >>> there's no way for the form to offer any customized validation, by way >>> of the clean() method. >> >>> def get_dept_weight_form(store): >>> fields = {} >>> s = Store.objects.get(pk=store.id) >>> for d in store.department_set.all(): >>> fields['id_%d' % d.id] = forms.DecimalField( >>> label = d.name, >>> initial = d.weight >>> ) >>> return type('WeightForm', (forms.BaseForm,), {'base_fields': >>> fields}) >> >>> Basically, this form spits out a label showing the department name and >>> the weight for you to enter in a textbox. The weights for all >>> departments should total 1.0. Should a clean() method handle this? >> >>> I already use an overridden version of save() on the store model, but >>> that just prints out a warning to stdout. Also, I have this >>> validation for the admin part by overriding the clean method for my >>> declared ModelForm. >> >>> Perhaps I should just use the functionality I already created in >>> models.py and just raise a ValidationError? >> >>> Thx >> >> > -- DjangoCon US 2010 September 7-9 http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: dynamic forms and custom methods
Hi Nick, Thanks for the reply. Yes, the department weights will be used in a grading scheme. One department may be 25% (0.25), etc... But in the end, the weights should total 1.0. For example, in my Store model: class Store(models.Model): #...etc... def _get_total_dept_weight(self): return self.department_set.all().aggregate(Sum('weight')) ['weight_sum'] def save(self, *args, **kwargs): if self._get_total_dept_weight() <> Decimal('1.0'): # fail super(Store, self).save(*args, **kwargs) So in the case of my custom form, I was thinking about raising a validation error if the totals submitted in the form don't total 1.0. But I was thinking over the weekend, maybe this isn't the best place for it after all. Maybe it should stay in the model because why does the form care if the weights is 1.0? The model does... Perhaps instead, raise a ValidationError exception from the Store's save() method? What are your thoughts? On Aug 11, 3:58 pm, Nickwrote: > Are you trying to create a save function that evaluates all the > weights and returns an error if they are more than 1? > > On Aug 10, 3:17 pm, lingrlongr wrote: > > > I 'm trying to create a form dynamically. This works just fine, but > > there's no way for the form to offer any customized validation, by way > > of the clean() method. > > > def get_dept_weight_form(store): > > fields = {} > > s = Store.objects.get(pk=store.id) > > for d in store.department_set.all(): > > fields['id_%d' % d.id] = forms.DecimalField( > > label = d.name, > > initial = d.weight > > ) > > return type('WeightForm', (forms.BaseForm,), {'base_fields': > > fields}) > > > Basically, this form spits out a label showing the department name and > > the weight for you to enter in a textbox. The weights for all > > departments should total 1.0. Should a clean() method handle this? > > > I already use an overridden version of save() on the store model, but > > that just prints out a warning to stdout. Also, I have this > > validation for the admin part by overriding the clean method for my > > declared ModelForm. > > > Perhaps I should just use the functionality I already created in > > models.py and just raise a ValidationError? > > > Thx > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: dynamic forms and custom methods
Are you trying to create a save function that evaluates all the weights and returns an error if they are more than 1? On Aug 10, 3:17 pm, lingrlongrwrote: > I 'm trying to create a form dynamically. This works just fine, but > there's no way for the form to offer any customized validation, by way > of the clean() method. > > def get_dept_weight_form(store): > fields = {} > s = Store.objects.get(pk=store.id) > for d in store.department_set.all(): > fields['id_%d' % d.id] = forms.DecimalField( > label = d.name, > initial = d.weight > ) > return type('WeightForm', (forms.BaseForm,), {'base_fields': > fields}) > > Basically, this form spits out a label showing the department name and > the weight for you to enter in a textbox. The weights for all > departments should total 1.0. Should a clean() method handle this? > > I already use an overridden version of save() on the store model, but > that just prints out a warning to stdout. Also, I have this > validation for the admin part by overriding the clean method for my > declared ModelForm. > > Perhaps I should just use the functionality I already created in > models.py and just raise a ValidationError? > > Thx -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
dynamic forms and custom methods
I 'm trying to create a form dynamically. This works just fine, but there's no way for the form to offer any customized validation, by way of the clean() method. def get_dept_weight_form(store): fields = {} s = Store.objects.get(pk=store.id) for d in store.department_set.all(): fields['id_%d' % d.id] = forms.DecimalField( label = d.name, initial = d.weight ) return type('WeightForm', (forms.BaseForm,), {'base_fields': fields}) Basically, this form spits out a label showing the department name and the weight for you to enter in a textbox. The weights for all departments should total 1.0. Should a clean() method handle this? I already use an overridden version of save() on the store model, but that just prints out a warning to stdout. Also, I have this validation for the admin part by overriding the clean method for my declared ModelForm. Perhaps I should just use the functionality I already created in models.py and just raise a ValidationError? Thx -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.