I have a similar set up to this (simplified):
class Transaction(models.Model):
sns = models.ManyToManyField(SN, related_name="transactions", blank=True)
qty = models.FloatField()
part = models.ForeignKey(Part, on_delete=models.CASCADE)
class InventoryTransaction(models.Model):
transaction = models.OneToOneField(Transaction, primary_key=True,
on_delete=models.CASCADE)
parent_sn = models.ForeignKey(SN, blank=True, null=True,
on_delete=models.CASCADE)
class Calculations(models.Model):
part = models.OneToOneField(Part, blank=False, null=False,
on_delete=models.CASCADE, primary_key=True)
i_qty = models.FloatField(blank=True, null=True)
Whenever the user creates a new inventory transaction or updates the qty in a
transaction that has an inventory transaction, I want to be able to change
Calculations.i_qty. I've been using pre_save signals on the
InventoryTransaction as the sender, but I've come to an obstacle.
When the user creates or deletes an inventory transaction, the calculation is
perfect. The problem comes when the user changes an inventory transaction
because the sender should actually be Transaction, and not
InventoryTransaction, since qty is on the Transaction field. Sometimes I may
update the Transaction instance without making any changes to the
InventoryTransaction instance, so the signal is never called to update
Calculations.i_qty.
However, if I modify the signal to change the sender to the Transaction model,
I get problems when creating an InventoryTransaction because the relation
hasn't been established yet so we don't know that it's actually an
InventoryTransaction that is being created. If I make a signal to handle both
Transaction and InventoryTransaction, the calculation is performed multiple
times, which provides bad results.
I've considered using transaction atomicity. Maybe I'm just missing something
in my signals. How do you typically handle this type of situation?
Thank you in advance!
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/698328c67a3f4e7f90321b95303b488f%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.