Django-Tastypie에서 사용자 정의 인증을 사용하고 있는데 인증이 성공하면 사용자 이름과 비밀번호를 반환합니다. 그 이름과 해당 암호는 메타 클래스에서 사용할 수 없기 때문에,Tastypie에서 Django 모델로 사용자 자격 증명 전달
class TestResource(ModelResource):
class Meta:
queryset = Test.remote_supported.get_all(username,password)
resource_name = 'test'
filtering = {
'id' : ALL,
}
detail_allowed_methods = ['get', 'post', 'patch']
authorization = Authorization()
authentication = MyCustomAuth()
always_return_data = True
def __init__(self, api_name=None):
self.username = None
self.password = None
super(TestResource, self).__init__(api_name)
def is_authenticated(self, request):
auth_result = self._meta.authentication.is_authenticated(request)
if isinstance(auth_result, HttpResponse):
raise ImmediateHttpResponse(response=auth_result)
if not auth_result is True:
raise ImmediateHttpResponse(response=http.HttpUnauthorized())
# this is where I receive the username and password from my custom auth
self.username, self.password = self._meta.authentication.get_credentials()
이 코드는 분명히 작동하지 않습니다, 그것은하더라도 : 인증이 성공되면 ModelResource 인스턴스에서, 사용자 이름과 암호를 사용할 수 있습니다 그것을 변경하는 것은이 인스턴스에만 영향을 미치지 않을 것이지만, 모든 인스턴스 (사용자의 의도가 아닌)는 사용자 이름과 비밀번호의 범위가 RESTful 쿼리마다 있어야하기 때문입니다.
이 내 모델이 모습입니다 :
class RemoteAuthSupported(models.Manager):
def get_all(self, username, password):
# ... here I do some custom operations with the username and password
return super(RemoteAuthSupported, self).get_query_set()
class Test(models.Model):
objects = models.Manager()
remote_supported = RemoteAuthSupported()
# ... the field declarations follow here ... #
나는 이것이 내가 자신의 인증을 필요로 내 장고 응용 프로그램과 비 ORM 데이터 소스를 사용하고 있다는 것입니다 수행하려고하는 이유, 동일한 사용자 이름과 비밀번호로 Tastypie ModelResource에서 Django Model로 전달하는이 매개 변수를 처리하는 방법은 무엇입니까? 아마도 여기에 사용중인 사용자 모델이 없다는 것을 언급해야합니다.
왜 'all()'을 사용해야합니까? [이 예] (https://groups.google.com/d/msg/django-tastypie/GxEVe0UTGgA/8Yyi2PmNknQJ)에 따르면 필수적인 것은 아닙니다. –
필수는 아니지만 tastypie는 'queryset' 속성에서 모델 클래스를 비롯한 일부 데이터를 유추합니다. 당신이 당신의 쿼리 세트에'get_all' 메소드를 추가한다면 당신은 언제나 ModelManager를 필요로하지 않고 모든 쿼리 세트를 필터링 할 수 있습니다. ModelManager에 따라 백업 된 커스텀 쿼리 세트 메소드는 실제로 삶을 더 쉽게 만듭니다. –