Friday, November 2, 2012

Admin, m2m relationship and custom save method: is save_related of any help?

Hi!

Snippet: http://dpaste.com/822354/

Idea: a product is made up of different components, each component
having a certain cost. I want a model for product and component. Given a
product I need to know its total cost.

Example: a car is made up of 4 wheels, 1 body and 1 engine. Given that
it's 100$ for a wheel, 1000$ for a body and 500$ for an engine, car
total cost sums up to 1900$.

In my snippet I use a m2m relationship through a ComponentQty model so
that I can tell how many components of the same type are needed, eg: 4
wheels for product car, 2 wheels for product motorbike.

In the admin I make use of a tabular inline to possibly create Product
and Component objects at the same time.

Then I defined a custom save method so that product total cost is
calculated and saved.

However, if I create a new product and some new components in the admin,
my custom save method will go pear shaped. That's because components
have not yet been created when product is saved. If I save my same
product a second time, then the save method will calculate total cost
correctly.

It looks like this is a known issue (or behaviour). I have heard Django
1.4 save_related should address this but I can't find no real example of
its use.

Could you help me and tell something more (possibly a real example)
about save_related? Do you think that could solve my problem?

Cheers, Fabio.

PS: would you please CC to my email address? fabio@fnstudio.it

--
Fabio Natali

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment