I wrote a RegistrationView for Djoser
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()
As you can see, I want to use my own create
function. That's why I use a ModelViewSet
But as you can see, I declare the queryset in a way where all the user objects will be listed, and I don't really like it.
So, my question.
Is there a way to declare another "queryset" that doesn't show that information?
Or should I write my "create" function in another place and don't write the queryset there? The point is that I want to call the create function in the registration process.
The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.
Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet . In other frameworks you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'.
Why don't you none the queryset if don't want to list all users.
queryset = User.objects.none()
Or you can override get_queryset method also.
def get_queryset(self):
qs = super(RegistrationView, self).get_queryset()
qs = qs.none()
return qs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With