Search code examples
djangodjango-rest-frameworkzapier

Django how to use webhook in django and send data to zapier?


I am beginner in django. i read the docuemntation of rest api. how to send my model detail to zapier and how to make webhook and configuration? kindly help me . how do i send my drivers to zapier and connect with zapier when my driver book a car

Models

class Booking(models.Model):
    class Meta():
        db_table = "booking"
        verbose_name = "Booking"
        verbose_name_plural = "Bookings"
        ordering = ['-booking_date']

    booking_car_car = models.ForeignKey(
        Car,
        on_delete=models.CASCADE,
        related_name='booking_car_car_key'
    )
    booking_customer_customer = models.ForeignKey(
        Customer,
        on_delete=models.CASCADE,
        related_name='booking_customer_customer'
    )
    booking_start_date = models.DateField(
        blank=False,
        null=False
    )
    booking_end_date = models.DateField(
        blank=False,
        null=False
    )
    booking_total_price = models.IntegerField(
        blank=False,
        null=False
    )
    booking_approved = models.NullBooleanField(
        blank=True,
        null=True
    )
    booking_date = models.DateTimeField(
        auto_now_add=True,
        blank=False,
        null=False
    )

    def __str__(self):
        return self.booking_customer_customer.customer_firstname

Solution

  • I've no previous experience with Zapier but, You could do an HTTP POST request to zapier url by overriding model's save() method.

    import requests
    
    class SampleModel(models.Model):
        # your fields
    
        def save(self, *args,**kwargs):
            if not self.pk: #send webhook on "EVERY OBJECT CREATION"
                webhook_data = your data to be sent to zapier
                requests.post(url=zapier_url,
                             data=webhook_data)