简体   繁体   English

Django:如何在pre_save信号中传递外键值?

[英]Django:How to pass a foreign key value in pre_save signal?

This are my models: 这是我的模型:

class Ledger1(models.Model):
    creation_Date   = models.DateField(default=datetime.date.today,blank=True, null=True)
    name            = models.CharField(max_length=32)
    group1_Name     = models.ForeignKey(Group1,on_delete=models.CASCADE,null=True,related_name='ledgergroups')

class Journal(models.Model):
    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)

This is my signal: 这是我的信号:

@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(date=instance.date, voucher_id=instance.id, voucher_type= "Journal",by=instance.by,to=Ledger1.objects.get(name='Profit & Loss A/c'),debit=instance.debit,credit=instance.credit)

I want to pass a pre_save signal that whenever a Journal objects is created which is having a Group1 object name of Indirect Expense another journal with the same by with the to value of 'Profit & Loss A/c' should be automatically created... 我想通过一个pre_save信号,每当Journal被创建的对象,其具有Group1的对象名称Indirect Expense使用相同的另一份杂志byto价值'Profit & Loss A/c'应该自动创建...

This line of code is giving me a error: 这行代码给我一个错误:

Ledger1.objects.get(name='Profit & Loss A/c')

Error: 错误:

get() returned more than one Ledger1 -- it returned 2!

Can anyone tell me how to get a Foreign key object in a pre_save signal? 谁能告诉我如何在pre_save信号中获取外键对象?

Thank you 谢谢

The get() is to be used when you expect only one result to be returned. 当您期望仅返回一个结果时,将使用get()。 So, you should use some other unique input which returns the only unique result you would expect. 因此,您应该使用其他一些唯一输入,该输入将返回您期望的唯一结果。

Alternatively, if you actually want more than 1 result to be returned, use the filter() method instead. 另外,如果您实际上希望返回多个结果,请改用filter()方法。 Something like, 就像是,

Ledger1.objects.filter(name='Profit & Loss A/c')

But in your case, it seems only 1 result is expected, so I recommend using a unique input or a workaround would be :- 但是在您的情况下,似乎只预期有1个结果,所以我建议使用唯一输入或解决方法:

Ledger1.objects.filter(name='Profit & Loss A/c').first()

This would return a single result which is only the first fetched result, if that's okay with the kind of logic you are trying to implement. 如果您尝试实现的逻辑类型可以,那么这将返回仅是第一个获取的结果的单个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM