Home > Software engineering >  How to put data from DRF form in function
How to put data from DRF form in function

Time:07-17

thank you for reading, i have problem in Django Rest Framework project, i wrote only backend, and i must return url with user info from fields:

models.py

class Payment(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.PROTECT)  # we cannot delete user with money
    
    # em
    email = models.CharField(max_length=255, blank=False, null=True)
    
    # m
    shop_id = models.CharField(max_length=30, blank=False, null=True)
    
    # oa
    amount = models.DecimalField(default=0, max_digits=12, decimal_places=2)

    class Curency(models.TextChoices):
        UAH = 'UAH'
        USD = 'USD'
        EUR = 'EUR'
        KZT = 'KZT'
        RUB = 'RUB'
        
    # currency
    currency = models.CharField(
        max_length=10,
        choices=Curency.choices,
        default=Curency.USD
    )
    
    # o
    order_id = models.CharField(
        max_length=255, null=True, blank=False, unique=False)
    
    # s
    sign = models.CharField(max_length=255, blank=False, null=True)
    
    url = models.CharField(max_length=1000, null=True)

    def __str__(self):
        return f'Payment {self.user} of game {self.order_id}'

serializers.py

class PaymentSerializer(serializers.ModelSerializer):
    # in order to work you should add related_name in Action model

    class Meta:
        model = Payment
        fields = '__all__'
        # balance is read only, because i don't want someone create account
        # with money
        read_only_fields = ('id', 'shop_id', 'sign', 'user', 'email')

views.py

class PaymentViewSet(viewsets.ModelViewSet,
                    mixins.CreateModelMixin,
                    mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.ListModelMixin,
                    ):
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated, )
    serializer_class = PaymentSerializer
    queryset = Payment.objects.all()
    
    def get_queryset(self):
        """Return object for current authenticated user only"""
        return self.queryset.all()
    
    def perform_create(self, serializer):
        serializer.save(user=self.request.user)
        serializer.save(email=self.request.user.email)
        shop_id = 'some id'
        secret1 = "secret1"
        amount = Payment.objects.get(amount) #there is problem
        currency = Payment.objects.get(currency) #there is problem
        order_id = Payment.objects.get(order_id) #there is problem
        list_for_sign = map(
            str, [shop_id, amount, secret1, currency, order_id])
        result_string = ":".join(list_for_sign).encode()
        sign_hash = hashlib.md5(result_string)
        sign = sign_hash.hexdigest()
        serializer.save(shop_id=shop_id)
        serializer.save(sign=sign)
        serializer.save(url="https://pay.freekassa.ru/?m={0}&oa={1}&o={2}&s={3}&em={4}&currency={5}".format(
            shop_id, amount, order_id, sign, self.request.user.email, currency))
        return sign

i wanna put info when i post it in form, but have problem:

UnboundLocalError at /pay/payments/
local variable 'amount' referenced before assignment

i understand, that i take order, amount and currency before i give it, but what i should do?)

Update, form in DRF looks like: DRF form looks like

CodePudding user response:

You can simply access the form data like so:

amount = serializer.validated_data['amount']

or

amount = self.request.data["amount"]

Then you can do whatever you want with those variables, like to use it to search in the database

  • Related