我为Djoser写了一个RegistrationView
class RegistrationView(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserRegistrationSerializer permission_classes = ( permissions.AllowAny, ) def perform_create(self, serializer): user = serializer.save() signals.user_registered.send(sender=self.__class__, user=user, request=self.request) if settings.get('SEND_ACTIVATION_EMAIL'): self.send_activation_email(user) elif settings.get('SEND_CONFIRMATION_EMAIL'): self.send_confirmation_email(user) def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) token = create_token(serializer.data) return Response(data=token, status=status.HTTP_201_CREATED, headers=headers) def send_activation_email(self, user): email_factory = utils.UserActivationEmailFactory.from_request(self.request, user=user) email = email_factory.create() email.send() def send_confirmation_email(self, user): email_factory = utils.UserConfirmationEmailFactory.from_request(self.request, user=user) email = email_factory.create() email.send()
如您所见,我想使用自己的create
功能.这就是我使用ModelViewSet的原因
但正如您所看到的,我以一种将列出所有用户对象的方式声明查询集,而我并不喜欢它.
所以,我的问题.
有没有办法声明另一个不显示该信息的"查询集"?
或者我应该在另一个地方编写我的"创建"功能,而不是在那里编写查询集?关键是我想在注册过程中调用create函数.
你为什么不无的查询集,如果不想列出所有用户.
queryset = User.objects.none()
或者您也可以覆盖get_queryset方法.
def get_queryset(self): qs = super(RegistrationView, self).get_queryset() qs = qs.none() return qs