Given Django Models that have a OneToOne relationship, how does one setup the Django Rest Framework Serializers & Views so that to the API consumer the end-point for paying in cash and paying in cheque include the payment model and allow for all CRUD functionality?
I've tried following this SO Question here but I can't seem to replicate the functionality.
Models
class Payment(models.Model):
"""
Payment Log
"""
merchant = models.ForeignKey(Entity, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_merchant")
customer = models.ForeignKey(Entity, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_customer")
payment_method = models.ForeignKey(PaymentMethod, on_delete=models.CASCADE)
trx_amount = models.DecimalField(max_digits=10, decimal_places=3)
class PaymentCash(models.Model):
"""
Cash payments
"""
payment = models.OneToOneField(Payment, on_delete=models.CASCADE, primary_key=True)
date_paid = models.DateField()
# Other fields
class PaymentCheque(models.Model):
"""
Cheque deposits
"""
payment = models.OneToOneField(Payment, on_delete=models.CASCADE, primary_key=True)
cheque_number = models.CharField(max_length=50)
# Other fields
Sample View
class PaymentCashViewSet(ListCreateRetrieveUpdateViewSet):
"""
Cash payment view
"""
queryset = PaymentCash.objects.all()
serializer_class = PaymentCashSerializer
permission_classes = (IsAuthenticated, HasPermission)
Current Serializer This though does not make use of all the fields in the Payment Model.
class PaymentCashSerializer(serializers.ModelSerializer):
"""Serialize model"""
class Meta:
model = PaymentCash
fields = ('payment', 'date_paid')
Using: Django==1.10.2 & djangorestframework==3.5.1