How to avoid duplicate object creation

Martins

I have duplication problem now I don't know the way forward need help I have this model branch with a reverse relation to stock

class Branch(models.Model):
    name = models.CharField(
        'Branch',
        unique=True,
        max_length=10,
        validators=[MinLengthValidator(5)])
    location = models.TextField(max_length=650,
        blank=True,
        help_text='location of the linsan branch')


    class Meta:
        verbose_name_plural = 'Branch'

    def __str__(self):
        return self.name 

class Stock(models.Model):
    branch          = models.ForeignKey(
        Branch,
        related_name = 'branch',
        help_text='Enter the name of the branch',
        on_delete=models.CASCADE,
        blank=False,
        null=False
        )

    manufacturers_name = models.CharField(
        max_length=50, 
        blank=True)

    description        = models.CharField(
        max_length=50,
        help_text='Enter the name of the product here',
        unique=True
        )

    model_number    = models.CharField(
        max_length=25,
        blank=True,
        null=True,
        help_text='Enter the model number of the item if any')

    color           = models.CharField(
        max_length=20,
        default='black',
        blank=True,
        null=True,)
    quantity        = models.PositiveIntegerField(
        validators=[validate],
        verbose_name='Quantity In stock')

    retail_price    = MoneyField(
        max_digits=14,
        decimal_places=2,
        default_currency='NGN',
        blank=False,
        verbose_name="Retail Price")

    customer_price  = MoneyField(
        max_digits=14,
        decimal_places=2,
        default_currency='NGN',
        blank=False)

    added = models.DateTimeField(
        auto_now_add=True, 
        help_text='The date the product was added')
    image           = models.ImageField(
        blank=True,
        null=True,
        upload_to='uploads/%Y/%m/%d/',
        help_text='Upload the image of the product if any')
    history = HistoricalRecords()
    class Meta:
        ordering = ['description']


    def __str__(self):
        return self.description

And another model with a foreignKey to stock

class WaybillItem(models.Model):
    waybill = models.ForeignKey(Waybill,
        on_delete=models.CASCADE,
        related_name='waybill_item')
    product = models.ForeignKey(
        'stock.Stock',
        on_delete=models.CASCADE,
        blank=False,
        null=False)
    quantity = models.PositiveIntegerField(validators=[validate, MinValueValidator(1)])

In my stock app I have a signal that I want to create new stock object to another branch if it doesn't exist.

def update_stock_on_waybill(sender, instance, **kwargs):
    transferred_to = instance.waybill.transferred_to.branch.all()
    product = instance.product

    if product in transferred_to:
        print("it is!")
        pass
    else:
        print("it isn't")
        Stock.objects.create(
            branch=instance.product.branch,
            description=instance.product.description,
            model_number=instance.product.model_number,
            color=instance.product.color,
            quantity=instance.quantity,
            retail_price=instance.product.retail_price,
            customer_price=instance.product.customer_price
            )
    product.save()

    pre_save.connect(update_stock_on_waybill, sender=WaybillItem)

But Every time I save a new Waybill(**I excluded the model) which doesn't exist it creates the new object and which is fine but same apply if the object does exist, I am relatively new to python, django, programming in general I just started so a nudge, a push, pointers in the right direction will be mostly grateful I will continue to search this site for something similar I believe someone might have stumbled across something similar preciously. Thanks in adv

krflol

To accomplish what you want, you can add a boolean check to the first pass.

def update_stock_on_waybill(sender, instance,is_create, **kwargs):

    transferred_to = instance.waybill.transferred_to.branch.all()
    product = instance.product

    if is_create:
        print("it is!")
        pass
    else:
        print("it isn't")
        Stock.objects.create(
            branch=instance.product.branch,
            description=instance.product.description,
            model_number=instance.product.model_number,
            color=instance.product.color,
            quantity=instance.quantity,
            retail_price=instance.product.retail_price,
            customer_price=instance.product.customer_price
            )
        is_create = True
    product.save()

    pre_save.connect(update_stock_on_waybill, sender=WaybillItem)
    return is_create

I added is_create as a parameter to the function, as the check, and as the return. is_created can be set as a variable outside of the scope of this function, then passed to the function every time it is called.

so

out_scope_created = False
out_scope_created = update_stock_on_waybill(sender,instance,out_scope_created)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related