0

i would like to know how i can save a field i get from json as dict. to my database field. Currently i only get

'int' object is not callable

thank and regards

6
  • Please add the full model and view and the full error traceback. Commented Mar 13, 2019 at 19:29
  • i added the field from models.py The rest is a snipped from a celery job so no views or templates only pure data processing. Commented Mar 13, 2019 at 19:35
  • 1
    What are you trying to do with user.acc_balance('confirmed')? As the error says, acc_balance is an integer, it does not know what to do with the 'confirmed' param that you are passing. Commented Mar 13, 2019 at 19:45
  • yes i guess this is my mistake i simply want to save the vaule of "confirmed" to the DB field "user.acc_balance" nothing more, nothing less. Commented Mar 13, 2019 at 19:46
  • What's with the update_or_create part? Do you want to save the data in the user that you have as user? If i understand correctly what you want is to save the confirmed value from decoded_balance to the user's acc_balance field. Is that correct? Commented Mar 13, 2019 at 19:59

1 Answer 1

1

I think this is what you are trying to do:

decoded_balance = json.loads(check_balance) 
user.acc_balance = decoded_balance["confirmed"]
user.save()

You already have the user, just update it's acc_balance field and save it.

Also, you might want to check how to parse json. For example, in this case you have something like this:

decoded_balance
# eg: {'confirmed': 2, 'unconfirmed': 3}
decoded_balance["confirmed"]
# 2
Sign up to request clarification or add additional context in comments.

2 Comments

okay and what if the value of confirmed changes from time to time or increases? thx so far :D
i don't know the larger scope of your code, but if you already have the user in a variable as in this case, you are updating the acc_balance field by assigning a new value and saving it. You would use update_or_create if you don't know for sure that the user exists, also you would need to pass all the other fields necessary to get or create a correct user, not just the acc_balance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.