Wednesday, September 30, 2020

Re: Add user to Group after purchase

Thanks Derek!

I tried using the signals but it is not working. This is how my signals and models looks:
from django.contrib.auth.models import User
from .models import *
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import Group 

@receiver(post_save, sender=User)
def subscription_group(senderinstancecreated, **kwargs):

        if OrderItem.product == 'Subscription_Product':
                Order.complete == True
                group = Group.objects.get(name='subscribees')
                print('New Subscribee Added')
from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Customer(models.Model):
    user = models.OneToOneField(User, null=Trueblank=Trueon_delete=models.CASCADE )
    name = models.CharField(max_length=200null=True)
    email = models.CharField(max_length=200null=True)
    phone = models.CharField(max_length=200null=True)
    profile_pic = models.ImageField(default="chicken_suit.gif" ,null=Trueblank=True)
    data_created = models.DateTimeField(auto_now_add=Truenull=True)

    def picURL(self):
                            url = self.profile_pic.url
                            url= ''
                    return url

    def __str__(self):

class Product(models.Model):
        name = models.CharField(max_length=200)
        price = models.FloatField()
        digital = models.BooleanField(default=Falsenull=Trueblank=True)
        image = models.ImageField(null=Trueblank=True)
        view = models.URLField(default=Falsenull=Trueblank=True)

        def __str__(self):
        def imageURL(self):
                            url = self.image.url
                            url= ''
                    return url

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=Trueblank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100null=True)

    def __str__(self):
        return str(

    def get_cart_total(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.get_total for item in orderitems])
        return total
    def get_cart_items(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.quantity for item in orderitems])
        return total

    def shipping(self):
        shipping= False
        orderitems = self.orderitem_set.all()
        for i in orderitems:
            if == False:
                    shipping = True
        return shipping

class 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=0null=Trueblank=True)
    date_added = models.DateTimeField(auto_now_add=True)
    tax= VALUE_ADDED_TAX/100

    def get_total(self):
            total = self.product.price * self.quantity + ( * self.product.price)
            return total

When 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
To view this discussion on the web visit

No comments:

Post a Comment