Reputation: 148
I would like to add an Item entry into my database in my django app, but I am having problems. I'm still learning django (who isn't?), but I've done db entries before. Part of this is because of things like cart instance, and contenttype instances.
Generally I start with...
item1 = Item(Cart(...), ContentType(...), quanity='4',<etc.>)
And depending on what I put in, it will let me do that, but when I do item1.save()
, it yells at me, and unfortunately the stack trace is hardly helpful. Or, maybe it's just me.
Any suggestions?
Upvotes: 0
Views: 174
Reputation: 118498
First suggestion is to post the stacktrace or even just the main exception; it's always more helpful to know what it's yelling.
My guess is first that you are passing in positional arguments and the model doesn't know what to do with which argument.
My second guess is that you are passing in unsaved instances Item(Cart()...)
to foreign key fields that are non nullable so django or the database would complain if you didn't pass in an actual Cart
instance with an ID defined.
So, explicitly define which fields you are passing to the constructor, and ensure you are passing in saved instances (not Cart()
but Cart.objects.get(id=X)
)
cart = Cart.objects.latest('id')
item = Item(cart=cart, etc.)
item.save()
Upvotes: 1