I am getting a runtime error in django while saving a certain model.
I want to save the model with two instances
So I have done the following:
class Journal(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True
)
company = models.ForeignKey(
company, on_delete=models.CASCADE, null=True, blank=True, related_name="Companyname"
)
counter = models.IntegerField(blank=True, null=True)
urlhash = models.CharField(max_length=100, null=True, blank=True, unique=True)
date = models.DateField(default=datetime.date.today)
voucher_id = models.PositiveIntegerField(blank=True, null=True)
voucher_type = models.CharField(max_length=100, blank=True)
by = models.ForeignKey(ledger1, on_delete=models.CASCADE, related_name="Debitledgers")
to = models.ForeignKey(ledger1, on_delete=models.CASCADE, related_name="Creditledgers")
debit = models.DecimalField(max_digits=10, decimal_places=2, null=True)
credit = models.DecimalField(max_digits=10, decimal_places=2, null=True)
narration = models.TextField(blank=True)
@receiver(pre_save, sender=Journal)
def pl_journal(sender, instance, *args, **kwargs):
if (
instance.debit != None
or instance.credit != None
or instance.by.group1_Name.group_Name == "Indirect Expense"
):
Journal.objects.update_or_create(
user=instance.user,
company=instance.company,
date=instance.date,
voucher_id=instance.id,
voucher_type="Journal",
by=instance.by,
to=ledger1.objects.filter(
user=instance.user, company=instance.company, name__icontains="Profit & Loss A/c"
).first(),
debit=instance.debit,
dredit=instance.credit,
)
The problem is in the following line of code in my signal:
to=ledger1.objects.filter(user=instance.user,company=instance.company,name__icontains='Profit & Loss A/c').first()
Anyone have any idea why this error is happening?
Is there any way to solve it?
Thank you
byobject in journal is created underinstance.by.group1_Name.group_Name == "Indirect Expense"it will trigger the signal to create another instance of journal model as given in the signal.