问题
I am new to Django signals and Shopify webhooks, but I want to implement this feature in to a project.
I am using this package, which also includes a set of WebhookSignals, to receive and verify the Shopify webhook, but then I want to do stuff with the information I receive (to be specific, I want to handle the customer information of an order and store it in a databse).
I believe I need to use the provided signals to do this, but I don't really understand how to. So far, I've tried to put a signals.py file in my project directory (together with settings.py) that looks the following:
from shopify_webhook.signals import orders_create
def my_callback(sender, **kwargs):
print("Request finished!")
orders_create.connect(my_callback)
This obviously doesn't work, but how would I define a function that gets called whenever I receive a webhook from Shopify?
回答1:
For what it's worth, I'd recommend just using the @webhook decorator directly on a view instead of unnecessarily complicating things with signals.
This is how your view would look:
from shopify_webhook.decorators import webhook
from myapp.models import AuthAppShopUser
@webhook
def orders_create(request):
user = AuthAppShopUser.objects.get(myshopify_domain=request.webhook_domain)
order_data = request.webhook_data
# The rest of your view here
The above example assumes that you're using django-shopify-auth for your user authentication, and have set up the user model AuthAppShopUser
in accordance to its documentation. You'll also want to be sure that you've registered the view to a url pattern within your urls.py
, and also registered the webhook to the store via the Shopify API.
来源:https://stackoverflow.com/questions/35408261/django-signals-with-shopify-webhooks