What kind of data do I pass into a Django Model.save() method?
Posted
by poswald
on Stack Overflow
See other posts from Stack Overflow
or by poswald
Published on 2010-06-01T05:10:01Z
Indexed on
2010/06/01
5:13 UTC
Read the original article
Hit count: 208
Lets say that we are getting POSTed a form like this in Django:
rate=10
items= [23,12,31,52,83,34]
The items are primary keys of an Item model. I have a bunch of business logic that will run and create more items based on this data, the results of some db lookups, and some business logic. I want to put that logic into a save signal or an overridden Model.save()
method of another model (let's call it Inventory
). The business logic will run when I create a new Inventory
object using this form data. Inventory will look like this:
class Inventory(models.Model):
picked_items = models.ManyToManyField(Item, related_name="items_picked_set")
calculated_items = models.ManyToManyField(Item, related_name="items_calculated_set")
rate = models.DecimalField()
... other fields here ...
New calculated_items
will be created based on the passed in items which will be stored as picked_items
.
My question is this: is it better for the save()
method on this model to accept:
- the request object (I don't really like this coupling)
- the form data as arguments or kwargs (a list of primary keys and the other form fields)
- a list of Items (The caller form or view will lookup the list of Items and create a list as well as pass in the other form fields)
- some other approach?
I know this is a bit subjective, but I was wondering what the general idea is. I've looked through a lot of code but I'm having a hard time finding a pattern I like.
© Stack Overflow or respective owner