My idea was centralize the validation in the Model to allow use it on
several clients besides the primary Web page (such as a batch program, a
web service, etc.)
At this moment, the database relationship yet not exists, but the
"memory data relashionship" already exists.
Conceptually I think that, how I can access the data that will be saved
on the Master class, I should be able to access the data that will be
save on related classes on the same transaction.
Now I've made a new test: I created a method "clean()" on Product class,
and see that from it I can't access data from Master class, and I can't
access from other instances of Product class...
Conceptually, in the Model the data of several instances of classes on
the same transaction can be treated as a set, or a object always be
individually validated on Model?
--
Fabiano Martins
Em 05-07-2012 22:03, Melvyn Sopacua escreveu:
On 5-7-2012 21:46, Fabiano Martins wrote:
I'm novice on Django, and I have a problem that I can't resolve through
documentation.
I like to make a validation on Model.clear() of the master class method based on
data of your detail classes, but it returns always a empty set.
This example illustrates my problem:
def clean(self):
super(Master, self).clean()
total = 0
for product in self.product_set.all():
total += product.value
At clean() time nothing has been saved, so the relationship isn't there
yet and you can't ask for it. There are a few ways to solve it in
decreasing order:
- Use a custom template and write some javascript that calculates the
master value on submit.
- Write a custom view and calculate the totals there.
- Wrap everything in a transaction, do the validation in the post_save
signal and roll back the transaction if the total is too low (not even
sure this /can/ be done).
Maybe others have more ideas.
--
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.