Fix payment creation

This commit is contained in:
fnux 2020-02-27 12:10:26 +01:00
parent 06ab21c577
commit f358acca05
2 changed files with 7 additions and 5 deletions

View file

@ -4,9 +4,9 @@ from .models import Bill, Payment
class BillSerializer(serializers.ModelSerializer):
class Meta:
model = Bill
fields = ['user', 'amount']
fields = ['owner', 'amount']
class PaymentSerializer(serializers.ModelSerializer):
class Meta:
model = Payment
fields = ['user', 'amount', 'source', 'timestamp']
fields = ['owner', 'amount', 'source', 'timestamp']

View file

@ -1,8 +1,10 @@
from django.shortcuts import render
from rest_framework import viewsets, permissions
from rest_framework import viewsets, permissions, status
from rest_framework.response import Response
from .models import Bill, Payment
from .serializers import BillSerializer, PaymentSerializer
from datetime import datetime
# to be implemented
@ -35,12 +37,12 @@ class PaymentViewSet(viewsets.ModelViewSet):
http_method_names = ['get', 'post']
def get_queryset(self):
return Payment.objects.filter(user=self.request.user)
return Payment.objects.filter(owner=self.request.user)
def create(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(user=request.user,timestamp=datetime.now())
serializer.save(owner=request.user,timestamp=datetime.now())
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)