-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to extend core components #285
Comments
Hi. |
thanks for reply. I'm still not sure what standard approach to use if I want to save foreign key to the user that makes order. I am not so active on related project so might be heading towards a dead end but I ended up reiplementing checkout app and hijacking different longclaw urls. To look at it, original question might be too broad to have an answer and I'm sorry if it is. |
Just a thought (untested but I've seen similar constructs applied to unrelated things)... but couldn't you write a middleware that listens for an order save signal and have access to the request there to create your foreign keys between your custom model, user, and the order? |
Another approach example: https://github.com/etianen/django-reversion/blob/master/reversion/middleware.py |
thanks, one could do that for small tweaks I suppose. I wouldn't tho, in my cases changes to checkout page grow quite a bit. |
Without a more specific example I am not sure how to help. Signals is kind of the way to add pluggable processing in Django. |
@thenewguy @dtwm Hello, In general, the best approach to extend core components in Django is to use Django's built-in mechanisms for customization, such as subclassing or monkey-patching. Here are a few possible ways to extend the Longclaw e-commerce platform:
For example, to add a foreign key to the user profile on the Order model, you could create a new model called from django.db import models
from longclaw.checkout.models import Order
class UserProfileOrder(Order):
user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE) Then, you can use
For example, to save additional data when creating a new order, you could create a signal handler that listens for the from django.db.models.signals import post_save
from django.dispatch import receiver
from longclaw.checkout.models import Order
@receiver(post_save, sender=Order)
def add_order_metadata(sender, instance, created, **kwargs):
if created:
# Add your additional data to the order here
instance.additional_data = ...
instance.save() This will automatically run the
For example, to modify the behavior of the from longclaw.checkout.views import create_order as original_create_order
def create_order(request):
# Add your custom logic here
...
# Call the original function to create the order
return original_create_order(request) This will intercept calls to the |
I can't wrap my head around what would be proper way to extend core models and/or alter build-in processes.
Simplest example -- I need to save additional data when creating new order or want to have a foreign key from order to user profile.
The only solid way that I see is custom fork of whole project which isn't that fun.
The text was updated successfully, but these errors were encountered: