Saturday, 15 January 2011

django - How should i deal with a type error, int() argument must be a string or a number? -



django - How should i deal with a type error, int() argument must be a string or a number? -

i have model orderitem,

class orderitem(smartmodel): shopping_id = models.charfield(max_length=50,db_index=true) quantity = models.integerfield(default=0) item = models.foreignkey(item) order = models.foreignkey(order)

and doing in function create object,

try: order= order.objects.get(pk=id) restaurant = restaurant.objects.get(pk=id) except: order = order(created_by=anon_user,modified_by=anon_user) oi=orderitem(order,shopping_id=_shopping_id(request), quantity=quantity, item=i, created_by=anon_user, modified_by=anon_user) oi.save()

i getting error int() argument must string or number, not 'order'. how should go this..

i can see various problems code. firstly you're using same id restaurant order -

order= order.objects.get(pk=id) restaurant = restaurant.objects.get(pk=id)

secondly you'll need save order before can pass foreign key order item. otherwise there won't primary key created on order, orderitem unable utilize in foreignkey field.

lastly, when initialize orderitem you're passing order arg rather kwarg.

your code should -

# order_id , restaurant_id try: order= order.objects.get(pk=order_id) restaurant = restaurant.objects.get(pk=restaurant_id) except: order = order(created_by=anon_user,modified_by=anon_user) order.save() oi=orderitem(order=order,shopping_id=_shopping_id(request), quantity=quantity, item=i, created_by=anon_user, modified_by=anon_user) oi.save()

as aside, might able substitue seek / except statement django's get_or_create shortcut function.

django django-models

No comments:

Post a Comment