I am working on Django Rest framework. I have created a few APIs and am storing data in sql lite. Now, I want to save data to CouchDB from the rest API calls basically crud application.
I am not getting how to connect to couch db via Django rest framework. I'm stuck here not getting how to do crud in couch db using Django rest api.
Below is one of the api written in django for adding accconts. I want to save this data in CouchDB.
models.py
class BankAccount(models.Model):
SAVINGS = 'saving'
CURRENT = 'current'
TYPE = (
(SAVINGS,'Saving'),
(CURRENT,'Current')
)
type = models.CharField(max_length=20, choices=TYPE, default=SAVINGS)
bank_name = models.CharField(max_length=200)
account_number = models.IntegerField()
balance = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
"""returns the model as string."""
return self.bank_name
def __str__(self):
"""returns the model as string."""
return self.type
serializers.py
class BankAccountSerializer(serializers.ModelSerializer):
class Meta:
model = BankAccount
fields = '__all__'
views.py
class BankAccountView(APIView):
def get_object(self, pk):
try:
return BankAccount.objects.get(pk=pk)
except BankAccount.DoesNotExist:
raise Http404
def get(self, request, format=None):
accounts = BankAccount.objects.all()
serializer = BankAccountSerializer(accounts, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = BankAccountSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, format=None):
data = json.loads(request.body)
pk = data['id']
action = data['action']
if action == 'delete':
return self.deleteItem(pk)
account = self.get_object(pk)
serializer = BankAccountSerializer(account, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def deleteItem(self, pk):
account = self.get_object(pk)
account.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Please help me I'm stuck.