Home > OS >  TypeError: unhashable type: 'dict' in Django
TypeError: unhashable type: 'dict' in Django

Time:12-21

I',m working on django rest project and i'm getting an err, i'm getting an error can anyone help, below is my code & d err msg

 class TrasactionSerializer(serializers.Serializer):
   amount = serializers.IntegerField(validators=[is_amount])
   email = serializers.EmailField()
   wallet = serializers.CharField(source='wallet.id', read_only=True)

  def validate_email(self, value):
      if CustomUser.objects.filter(email=value).exists():
          return value
      raise serializers.ValidationError({"detail": "Email not found"})

  def save(self):
    user = self.context['request'].user
    wallet = WalletModel.objects.get(user=user)
    data = self.validated_data
    url = 'https://api.paystack.co/transaction/initialize'
    headers = {
        "authorization": f"Bearer {settings.PAYSTACK_SECRET_KEY}"
    }
    r = requests.post(url, headers=headers, data=data)
    response = r.json()
    walletTransaction.objects.create(
        wallet=wallet,
        transaction_type="deposit",
        amount= data["amount"],
        paystack_payment_reference=response['data']['reference'],
        status=response["pending"],
    )

err message

  File "/home/olaneat/Desktop/files/project/django/jobConnect/job-connect/wallet/serializers.py", line 59, in save
    headers = {
TypeError: unhashable type: 'dict'

can anyone help pls

CodePudding user response:

headers = {
    {"authorization": f"Bearer {settings.PAYSTACK_SECRET_KEY}"}
}

That is a set containing a dictionary. Don't do that.

Just make a normal dictionary:

headers = {
    "authorization": f"Bearer {settings.PAYSTACK_SECRET_KEY}"
}
  • Related