How to use create function instead of perform create in ListCreateApiView in django rest framework

0 votes

I am trying to create a booking api for a website.

For this, I used perform_create function in ListCreateApiView. But, it was someone else who helped me and told me to use perform_create function.

But, I was thinking, it should be possible using create function and is a right approach rather than perform_create function.

Also, I don't really know the difference between these two functions and don't really know when to use which

Here is my code:

    class BookingCreateAPIView(ListCreateAPIView):
        permission_classes= [IsAuthenticated]
        queryset = Booking.objects.all()
        serializer_class = BookingSerializer
    
        def perform_create(self, serializer):
            # user = self.request.user
            package = get_object_or_404(Package, pk= self.kwargs['pk'])
            serializer.save(user=self.request.user,package=package)

Here is my serializer:

    class BookingSerializer(serializers.ModelSerializer):
        # blog = serializers.StringRelatedField()
        class Meta:
            model = Booking
            fields = ['name', 'email', 'phone', 'bookedfor']

Here is my model:

    class Booking(models.Model):
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        package = models.ForeignKey(Package, on_delete=models.CASCADE, related_name='package')
        name = models.CharField(max_length=255)
        email = models.EmailField()
        phone = models.CharField(max_length=255)
        bookedfor = models.DateField()
        created_at = models.DateTimeField(auto_now_add=True)
    
    
        class Meta:
            ordering = ('created_at',)
Nov 30, 2020 in Python by anonymous
• 120 points
3,552 views

Hi, @There,

Is there any error you are facing while executing your code?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

How to return custom JSON in Django REST Framework?

Hello @kartik, There are 2 ways to custom ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
11,162 views
0 votes
1 answer

How to add annotate data in django-rest-framework queryset responses?

Hello @kartik, The queryset returned from get_queryset provides ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
6,126 views
0 votes
1 answer

How to secure APIs for Registration and Login in Django Rest Framework?

Hello @kartik, you cannot have an authentication system ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
2,879 views
0 votes
1 answer

How to disable a method in a ViewSet, django-rest-framework

Hello @kartik, You could keep using viewsets.ModelViewSet and define http_method_names on your ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,840 points
10,850 views
0 votes
1 answer

How to update user password in Django Rest Framework?

Hello @kartik, Using a modelserializer might be an ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,840 points
9,239 views
0 votes
1 answer

How to order_by a JSON from serializers.py file in django rest framework?

Hello @kartik, There's an easy way, just override ...READ MORE

answered Jul 2, 2020 in Python by Niroj
• 82,840 points
5,871 views
0 votes
1 answer

how to download and install Django rest framework?

To install Django, you can simply open ...READ MORE

answered Apr 24, 2018 in Python by Christine
• 15,790 points
1,969 views
0 votes
1 answer
0 votes
1 answer

Host not allowed

Go to your project directory cd project cd project ALLOWED_HOSTS ...READ MORE

answered Aug 9, 2018 in AWS by Priyaj
• 58,020 points
1,742 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP