0

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
nayaz jh
  • 21
  • 1
  • 6
  • Are you using couchdb in addition to SQLite, or are you wanting to replace SQLite with couchdb? – Will Keeling May 08 '18 at 12:40
  • i want to replace SQlLite with couch db – nayaz jh May 08 '18 at 12:42
  • Hi can anyone please help me out – nayaz jh May 09 '18 at 05:36
  • @WillKeeling do you have any idea? – nayaz jh May 09 '18 at 18:37
  • Swapping SQLite with CouchDB is likely to be non-trivial, as CouchDB is not a relational database like SQLite and so you will likely hit conceptual incompatibilities. I don't have any direct experience of using Django with CouchDB, but it looks as though there are [backends](https://stackoverflow.com/questions/2169737/what-is-the-best-couchdb-backend-for-django) available as well as some [tutorials](https://lethain.com/an-introduction-to-using-couchdb-with-django/). Sorry I cannot be of more help. – Will Keeling May 09 '18 at 18:47

2 Answers2

0

Django is in Python and there is python-cloudant which is well-maintained and might help you use CouchDB with Python.

Megidd
  • 7,089
  • 6
  • 65
  • 142
0

The Apress book "Beginning CouchDB" by Joe Lennon (pub 2009) has an example app that uses CouchDB as the backend for a Django app via couchdbkit. Sadly, it's a local database in "Admin Party" mode, so I have yet to work out how to add the needed parameters to connect it to Cloudant. Doing command line python examples with Cloudant works fine for me - there are tutorials explaining that, and it works with couchdbkit.

In Joe's example, he leaves a minimal default SQLite entry in settings.py as a "dummy" placeholder.

There are clues in the couchdbkit library, leading me to think I need to construct and pass a URI string (perhaps as a value in the COUCHDB_DATABASES parameter in settings.py) with content similar to IBM's VCAP_SERVICES environment variable with credentials as key-value pairs. I have not found any usage examples.

charles ross
  • 570
  • 4
  • 15