2014-09-07 3 views
0

나는 다음과 같은 전망을 - 가져 오기가 잘 작동하고 최대한 빨리처럼 그러나 '제독'데이터베이스에서 데이터를 당기고 장고 나머지 프레임 워크

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) 

웹 페이지의 게시물은 '기본'데이터베이스에 저장하려고하기 때문에 테이블이 존재하지 않는다는 오류를 반환합니다. 특정 데이터베이스에 명시 적으로 저장하고 있기 때문에 왜이 작업을 수행하는지 잘 모르겠습니다. 여기에 흔적이 있습니다 -

여기서는 sqlite3 데이터베이스가 기본 데이터베이스임을 유의하겠습니다. 'Admiral'데이터베이스는 SQL Server 데이터베이스입니다. 그러므로 나는 using = '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 

답변

0

라우터를 만들어이 문제를 해결해야했습니다. 이 'Admiral'db를 참조하는 특정 라우터를 만든 다음 API '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 
관련 문제