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, Nick <nickt...@gmail.com> 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 <keith.ebe...@gmail.com> 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.

Reply via email to