简体   繁体   English

使用Django Rest Framework 3.6.2和python 3连接到Couchdb

[英]Connect to Couchdb using Django rest framework 3.6.2 and python 3

I am working on Django Rest framework. 我正在研究Django Rest框架。 I have created a few APIs and am storing data in sql lite. 我创建了一些API,并将数据存储在sql lite中。 Now, I want to save data to CouchDB from the rest API calls basically crud application. 现在,我想将其余API调用(基本上是原始应用程序)的数据保存到CouchDB中。

I am not getting how to connect to couch db via Django rest framework. 我没有得到如何通过Django rest框架连接到沙发数据库。 I'm stuck here not getting how to do crud in couch db using Django rest api. 我被困在这里,没有得到如何使用Django rest api在沙发上做杂物。

Below is one of the api written in django for adding accconts. 以下是用django编写的用于添加accconts的api之一。 I want to save this data in CouchDB. 我想将此数据保存在CouchDB中。

models.py 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. 请帮我,我卡住了。

Django使用的是Python,并且其中的python-cloudant维护良好,可以帮助您将CouchDB与Python结合使用。

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. Joe Lennon(出版于2009年)的Apress书“ Beginning CouchDB ”中有一个示例应用程序,该应用程序通过CouchDBKit将CouchDB用作Django应用程序的后端。 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. 遗憾的是,它是处于“管理方”模式下的本地数据库,因此我尚未弄清楚如何添加所需的参数以将其连接到Cloudant。 Doing command line python examples with Cloudant works fine for me - there are tutorials explaining that, and it works with couchdbkit. 使用Cloudant进行命令行python示例对我来说效果很好-有一些教程对此进行了解释,并且可以与couchdbkit一起使用。

In Joe's example, he leaves a minimal default SQLite entry in settings.py as a "dummy" placeholder. 在Joe的示例中,他将最小的默认SQLite条目留在settings.py中作为“虚拟”占位符。

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. benchdbkit库中有一些线索,使我认为我需要构造并传递一个URI字符串(可能是settings.py中COUCHDB_DATABASES参数中的一个值),其内容类似于IBM的VCAP_SERVICES环境变量,其凭证为键值对。 I have not found any usage examples. 我还没有找到任何用法示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM