Question about Django rest framework Serializers and Views working principles
Question:
I’m trying to build REST api with Django Rest Framework and kind a having diffuculty to understand how things connected each other in terms of when we need to use the custom functions.
I have views.py
like this
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
this is my serializer.py
class myserializer(serializers.ModelSerializer):
class Meta:
model = myTable
fields = "__all__"
def create(self, validated_data):
#doing some operation here and save validated data
def update(self, instance, validated_data):
#doing some operation here and save validated data
I want to add some custom function to do let’s say sending emails with processed data.
so when I add my_email_sender
function to nothing happens (nothing prints to terminal).
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
def my_email_func():
print("Hey I'm email function")
my_email_sender()
OTH, when do do this inside of the serializer
its printing to the screen.
I actually really don’t know this my_email_func
should be inside of views.py
some sort of CRUD
operation function like def create(), def update() etc..
I also don’t know why we cannot call it from views.py ?
Thank for your answer in advance!
Answers:
You just have to create a get
function, and call your function inside of it
it should be something like this:
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
def my_email_func():
print("Hey I'm email function")
my_email_sender()
def get(self, request, *args, **kwargs):
my_email_func()
return Response(status=status.HTTP_200_OK)
and then create a URL in the urls.py
that references this view and call the endpoint with get method form postman or the browser.
I’m trying to build REST api with Django Rest Framework and kind a having diffuculty to understand how things connected each other in terms of when we need to use the custom functions.
I have views.py
like this
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
this is my serializer.py
class myserializer(serializers.ModelSerializer):
class Meta:
model = myTable
fields = "__all__"
def create(self, validated_data):
#doing some operation here and save validated data
def update(self, instance, validated_data):
#doing some operation here and save validated data
I want to add some custom function to do let’s say sending emails with processed data.
so when I add my_email_sender
function to nothing happens (nothing prints to terminal).
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
def my_email_func():
print("Hey I'm email function")
my_email_sender()
OTH, when do do this inside of the serializer
its printing to the screen.
I actually really don’t know this my_email_func
should be inside of views.py
some sort of CRUD
operation function like def create(), def update() etc..
I also don’t know why we cannot call it from views.py ?
Thank for your answer in advance!
You just have to create a get
function, and call your function inside of it
it should be something like this:
class myAPIView(viewsets.ModelViewSet):
queryset = myTable.objects.all()
serializer_class = mySerializer
def my_email_func():
print("Hey I'm email function")
my_email_sender()
def get(self, request, *args, **kwargs):
my_email_func()
return Response(status=status.HTTP_200_OK)
and then create a URL in the urls.py
that references this view and call the endpoint with get method form postman or the browser.