title: Webhooks with Python and Django
body: <p>I have a subscription model:</p>
  
  <pre><code>class Subscription(models.Model):
  
  STATUS_CURRENT = ["trialing", "active"]
  
  plan_amount = models.CharField(max_length=120, null=True, blank=True)
  plan_id = models.CharField(max_length=120, null=True, blank=True)
  subscription_id = models.CharField(max_length=120)
  subscription_owner = models.OneToOneField(Provider, related_name='subscription')
  cancel_at_period_end = models.BooleanField(default=False)
  canceled_at = models.DateTimeField(blank=True, null=True)
  current_period_end = models.DateTimeField(blank=True, null=True)
  current_period_start = models.DateTimeField(blank=True, null=True)
  ended_at = models.DateTimeField(blank=True, null=True)
  status = models.CharField(max_length=25)# trialing, active, past_due, canceled, or unpaid
  
  def status_display(self):
      return self.status.replace("_", " ").title()
  
  def __str__(self):
      return self.subscription_id
  </code></pre>
  
  <p>I also have the following stripe webhook set up and have no problems with that:</p>
  
  <pre><code>@require_POST
  @csrf_exempt
  def subscription_cancelled_callback(request):
  # Process webhook data in `request.body and parse it as JSON`
  stripe_payload = json.loads(request.body)
  
  print('cancelled')
  
  return HttpResponse(status=200)
  </code></pre>
  
  <p>What I am trying to achieve is update my subscription object for example update the status of the subscription in my backend with the json returned through the webhook, i.e "active" or "cancelled"</p>
  
  <p>New to using webhooks but not sure how to update the object from the webhook json. Any pointers would be a great help.</p>
  
  <p>This is an example of the webhook json on a test webhook:
      {
    "created": 1326853478,
    "livemode": false,
    "id": "evt_00000000000000",
    "type": "customer.subscription.deleted",
    "object": "event",
    "request": null,
    "pending_webhooks": 1,
    "api_version": "2017-02-14",
    "data": {
      "object": {
        "id": "sub_00000000000000",
        "object": "subscription",
        "application_fee_percent": null,
        "billing": "charge_automatically",
        "cancel_at_period_end": false,
        "canceled_at": null,
        "created": 1510580811,
        "current_period_end": 1513172811,
        "current_period_start": 1510580811,
        "customer": "cus_00000000000000",
        "discount": null,
        "ended_at": 1510598517,
        "items": {
          "object": "list",
          "data": [
            {
              "id": "si_BlEkx6XqXqAgh1",
              "object": "subscription_item",
              "created": 1510580811,
              "metadata": {
              },
              "plan": {
                "id": "pro-monthly",
                "object": "plan",
                "amount": 9999,
                "created": 1509374183,
                "currency": "usd",
                "interval": "month",
                "interval_count": 1,
                "livemode": false,
                "metadata": {
                },
                "name": "Pro Plan",
                "statement_descriptor": null,
                "trial_period_days": null
              },
              "quantity": 1
            }
          ],
          "has_more": false,
          "total_count": 1,
          "url": "/v1/subscription_items?subscription=sub_BlEk5E9CBuFdTJ"
        },
        "livemode": false,
        "metadata": {
        },
        "plan": {
          "id": "pro-monthly_00000000000000",
          "object": "plan",
          "amount": 9999,
          "created": 1509374183,
          "currency": "usd",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "name": "Pro Plan",
          "statement_descriptor": null,
          "trial_period_days": null
        },
        "quantity": 1,
        "start": 1510580811,
        "status": "canceled",
        "tax_percent": null,
        "trial_end": null,
        "trial_start": null
      }
    }
  }</p>