Search code examples
djangodjango-modelssignalsdjango-signals

Create objects from one model based on new object in another model (Django)


Fairly new to Django (1.2), but just testing stuff out.

My headache is figuring out a way to create a series of SKU objects when a Product object is created.

class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    style = models.CharField(max_length=50)
    colors = models.ManyToManyField(Color)
    sizes = models.ManyToManyField(Size)

class SKU(models.Model):
    id = models.CharField(max_length=15, unique=True, primary_key=True)
    style = models.ForeignKey(Product)
    color = models.ForeignKey(Color)
    size = models.ForeignKey(Size)
    quantity = models.IntegerField(default=0)

I initially tried to handle this in Product.save, but alas, no dice. I then tried this signals setup:

# models.py
def create_skus(sender, instance, *args, **kwargs):
    if 'created' in kwargs:
        if kwargs['created']:
            for x in instance.colors.select_related():
                for y in instance.sizes.select_related():
                    sku = instance.style
                    sku += instance.x.shortcode
                    sku += instance.y.name
                    s = SKU(id=sku, style=instance, color=x, size=y)
                    s.save()

... # after the Product class
post_save.connect(create_skus, sender=Product)

Some examples I've seen call for the sender class save method to have something like this:

def save(self):
    post_save.send(create_skus, self)

...and that's where I'm at. This setup doesn't throw any errors, but it also doesn't create said SKUs.

If I'm way off on any of this signals-wise, any pointers would be appreciated. Or if there's another technique to accomplish a similar feat, that would be wonderful as well.

Cheers!


Solution

  • def create_skus(sender, instance, *args, **kwargs):
            if kwargs['created']:
                for color in instance.colors.select_related():
                    for size in instance.sizes.select_related():
                        sku = u'%s%s%s' % (instance.style, color.shortcode, size.name) 
                        s = SKU(id=sku, style=instance, color=color, size=size)
                        s.save()
    
    post_save.connect(create_skus, sender=Product)
    

    This should work? If not, try installing the Debug Toolbar and checking that select_related() is returning the results you are expecting.