django rest框架保存(使用='数据库')无效

时间:2014-09-07 00:41:20

标签: django django-rest-framework

我有以下观点 -

class DeployResourceFilterView(generics.ListAPIView):
    serializer_class = ResourceSerializer

    def get(self, request, format=None):
        resname = self.request.GET.get('name')
        queryset = Resmst.objects.using('Admiral').filter(resmst_name=resname)
        serializer = ResourceSerializer(queryset, many=True)
        if queryset:
            return Response(serializer.data)
        else:
            raise Http404

    def post(self, request, format=None):
        serializer = ResourceSerializer(data=request.DATA, many=True)
        if serializer.is_valid():
            serializer.save(using='Admiral')
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

GET工作正常,正在从“海军上将”中提取数据。数据库,但是当我从网页上发帖子时,它会返回一个表格不存在的错误,因为它正在尝试保存到默认状态'数据库。我不知道为什么要这样做,因为我明确地保存到特定的数据库。这是追溯 -

我在这里注意到sqlite3数据库是默认数据库。海军上将' database是一个sql server数据库。因此,我知道使用=' Admiral'没有用。

Traceback:
File "D:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "D:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  400.             response = self.handle_exception(exc)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  397.             response = handler(request, *args, **kwargs)
File "D:\Tidal\API\views.py" in post
  311.         if serializer.is_valid():
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in is_valid
  553.         return not self.errors
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in errors
  535.                         ret.append(self.from_native(item, None))
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in from_native
  996.         instance = super(ModelSerializer, self).from_native(data, files)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in from_native
  368.             attrs = self.restore_fields(data, files)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in restore_fields
  283.                 field.field_from_native(data, files, field_name, reverted_data)
File "D:\Python27\lib\site-packages\rest_framework\relations.py" in field_from_native
  189.             into[(self.source or field_name)] = self.from_native(value)
File "D:\Python27\lib\site-packages\rest_framework\relations.py" in from_native
  228.             return self.queryset.get(pk=data)
File "D:\Python27\lib\site-packages\django\db\models\manager.py" in get
  151.         return self.get_queryset().get(*args, **kwargs)
File "D:\Python27\lib\site-packages\django\db\models\query.py" in get
  304.         num = len(clone)
File "D:\Python27\lib\site-packages\django\db\models\query.py" in __len__
  77.         self._fetch_all()
File "D:\Python27\lib\site-packages\django\db\models\query.py" in _fetch_all
  857.             self._result_cache = list(self.iterator())
File "D:\Python27\lib\site-packages\django\db\models\query.py" in iterator
  220.         for row in compiler.results_iter():
File "D:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  713.         for rows in self.execute_sql(MULTI):
File "D:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  786.         cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\utils.py" in __exit__
  99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "D:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  451.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /deploy/resource/
Exception Value: no such table: owner

1 个答案:

答案 0 :(得分:0)

我必须通过创建路由器来解决这个问题。我做了一个特定的路由器引用这个'Admiral'数据库,然后将它分配给应用程序'API'。

settings.py

DATABASE_ROUTERS = [ 'routers.DefaultRouter', 'routers.AdmiralRouter', 'routers.TIDALRouter' ]

routers.py

class TIDALRouter(object):
    """
    A router to control all database operations on models in the
    auth application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth_db.
        """
        if model._meta.app_label == 'API':
            return 'Admiral'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to Admiral.
        """
        if model._meta.app_label == 'API':
            return 'Admiral'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        if obj1._meta.app_label == 'API' or \
           obj2._meta.app_label == 'API':
           return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the auth app only appears in the 'Admiral'
        database.
        """
        if db == 'Admiral':
            return model._meta.app_label == 'API'
        elif model._meta.app_label == 'API':
            return False
        return None