from django.db.models.signals import post_save
class MyModel(Model):
id = AutoField(primary_key=True)
# etc.
def MyModel_postsave_handler(sender, **kwargs):
obj = kwargs['instance'] # object that was saved!
# add logic ...
post_save.connect(
MyModel_postsave_handler,
sender=MyModel,
dispatch_uid="mymodel.models")
I hope this simple example can help? Try to use pdb to see if the postsave_handler function is reached.
On Thursday, 1 October 2020 at 01:04:55 UTC+2 Lightning Bit wrote:
Thanks Derek!I tried using the signals but it is not working. This is how my signals and models looks:Signals.py:from django.contrib.auth.models import Userfrom .models import *from django.db.models.signals import post_savefrom django.dispatch import receiverfrom django.contrib.auth.models import Group@receiver(post_save, sender=User)def subscription_group(sender, instance, created, **kwargs):if OrderItem.product == 'Subscription_Product':Order.complete == Truegroup = Group.objects.get(name='subscribees')User.groups.add(group)print('New Subscribee Added')Models.py:from django.db import modelsfrom django.contrib.auth.models import User# Create your models here.class Customer(models.Model):user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE )name = models.CharField(max_length=200, null=True)email = models.CharField(max_length=200, null=True)phone = models.CharField(max_length=200, null=True)profile_pic = models.ImageField(default="chicken_suit.gif" ,null=True, blank=True)data_created = models.DateTimeField(auto_now_add=True, null=True)@propertydef picURL(self):try:url = self.profile_pic.urlexcept:url= ''return urldef __str__(self):return self.nameclass Product(models.Model):name = models.CharField(max_length=200)price = models.FloatField()digital = models.BooleanField(default=False, null=True, blank=True)image = models.ImageField(null=True, blank=True)view = models.URLField(default=False, null=True, blank=True)def __str__(self):return self.name@propertydef imageURL(self):try:url = self.image.urlexcept:url= ''return urlclass Order(models.Model):customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)date_ordered = models.DateTimeField(auto_now_add=True)complete = models.BooleanField(default=False)transaction_id = models.CharField(max_length=100, null=True)def __str__(self):return str(self.id)@propertydef get_cart_total(self):orderitems = self.orderitem_set.all()total = sum([item.get_total for item in orderitems])return total@propertydef get_cart_items(self):orderitems = self.orderitem_set.all()total = sum([item.quantity for item in orderitems])return total@propertydef shipping(self):shipping= Falseorderitems = self.orderitem_set.all()for i in orderitems:if i.product.digital == False:shipping = Truereturn shippingclass OrderItem(models.Model):product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)quantity = models.IntegerField(default=0, null=True, blank=True)date_added = models.DateTimeField(auto_now_add=True)VALUE_ADDED_TAX = 6.0tax= VALUE_ADDED_TAX/100@propertydef get_total(self):total = self.product.price * self.quantity + (self.tax * self.product.price)return totalWhen an individual purchases, the signal is not received and that individual is not added to the "subscribees" group.Any further suggestions?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0aa83a26-77b0-421d-93b2-d9ad5b2fcaf7n%40googlegroups.com.
No comments:
Post a Comment