Django update "extended user profile" model field from webhook paramater

I am getting a webhook from our billing processor after a user makes a payment. So far I have successfully saved the webhook in models for record keeping. However, now I need to update the User account_paid field to the appropriate status.

I believe I've outlined the steps properly but I am stuck on implementing the code. How do I update account_paid and make sure it's the right user ID?

def webhook(request):
    template_name = 'payment/index.html'

    hook = Webhook()
    hook.user = request.GET.get('clientAccnum')
    hook.clientSubacc = request.GET.get('clientSubacc')
    hook.eventType = request.GET.get('eventType')
    hook.eventGroupType = request.GET.get('eventGroupType')
    hook.subscriptionId = request.GET.get('subscriptionId')

    hook.timestamp = request.GET.get('timestamp')
    hook.timestamplocal =

    print (hook.user, hook.clientSubacc, hook.timestamplocal)

    if hook.eventType == 'RenewalSuccess':
        #Update user account_level to Paid Account

        #Update user account_level to Free

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    account_level = models.BooleanField(default=False)

There are no error messages yet because I'm trying to figure out the structure right now. The goal is to finish this question with a working solution.

*Side note: I know webhooks are delivered to URL as POST but for now I am using get purely for debugging purposes.

1 answer

  • answered 2018-01-13 17:54 wm3ndez

    The Profile model has a OneToOne relationship with User, so you can simply say:
    hook.user.profile.account_paid = hook.eventType == 'RenewalSuccess'