Redis TypeError:必须是字符串或缓冲区,而不是None。

时间:2017-01-03 11:35:47

标签: python django django-models redis

我对redis完全不熟悉,如果问题太基础,请接受我的道歉。

我想在我的redis数据存储中设置一个django模型,然后在另一个视图函数中,我想得到它并再次重用它;但事实证明:

  

TypeError:必须是字符串或缓冲区,而不是None。

这是views.py中的代码:

connection = redis.Redis('localhost')    
def recharge_account(request):

     cur = recharge_form.cleaned_data['currency']
                amnt = recharge_form.cleaned_data['amount']
                user_profile = models.UserProfile.objects.get(user=models.User.objects.get(id=request.user.id))
                user_b_account, created = models.BankAccount.objects.get_or_create(
                    owner=user_profile,
                    cur_code=cur,
                    method=models.BankAccount.DEBIT,
                    name=request.user.username + '_' + cur + '_InterPay-account',
                    account_id=make_id()
                )
                #  saving the temporarily deposit in redis
                deposit_dict = {"account": user_b_account, "amount": amnt, "banker": user_profile,
                                "date": user_b_account.when_opened, "cur_code": cur}
                pickled_deposit_dict = pickle.dumps(deposit_dict)
                cached_deposit_name = str(user_profile.user_id) + "-cachedDeposit"
                connection.set(cached_deposit_name, pickled_deposit_dict)

    ....    




 def callback_handler(request, amount):
    #getting from redis
                        new_deposit = pickle.loads(
                            connection.get(str(user_profile.user_id) + "-cachedDeposit")) 
                        deposit = models.Deposit(account=new_deposit['account'], amount=new_deposit['amount'],
                                                 banker=new_deposit['banker'],
                                                 date=new_deposit['date'], cur_code=new_deposit['cur_code'])
                        deposit.save()
                       .....

我的问题是:我的手术有问题吗?在redis中保存django模型有什么问题吗?

我该怎么做才能使这个功能正常工作并使保存的数据“不是无”?

先谢谢

0 个答案:

没有答案