Python .delete()不处理从signals.py Django调用的记录
我正在创建一个计费面板。在将重复的帐单地址分配给订单实例的部分中,它会自动删除,并将以前的帐单地址分配给订单。虽然前一个账单实例被分配给订单,但重复的账单地址实例不会被删除。我是个笨蛋。模型中没有重写的delete方法 models.py:-Python .delete()不处理从signals.py Django调用的记录,python,django,Python,Django,我正在创建一个计费面板。在将重复的帐单地址分配给订单实例的部分中,它会自动删除,并将以前的帐单地址分配给订单。虽然前一个账单实例被分配给订单,但重复的账单地址实例不会被删除。我是个笨蛋。模型中没有重写的delete方法 models.py:- class BillingAddress(models.Model): #Contains address and email and other info class Order(models.Model): billingAd
class BillingAddress(models.Model):
#Contains address and email and other info
class Order(models.Model):
billingAddress = billingAddress = models.ForeignKey(BillingAddress,
null=True,on_delete=models.SET_NULL
)
signals.py:-
@receiver(post_save, sender=Order)
def dont_save_if_all_fields_matching(sender, instance, created, **kwargs):
if created:
currentOrder = instance
previousBilling = BillingAddress.objects.filter(user=currentOrder.user,
firstname=currentOrder.billingAddress.firstname,
lastname=currentOrder.billingAddress.lastname,
phonenumber=currentOrder.billingAddress.phonenumber,
address1=currentOrder.billingAddress.address1,
address2=currentOrder.billingAddress.address2,
city=currentOrder.billingAddress.city,
country=currentOrder.billingAddress.country,
state=currentOrder.billingAddress.state,
pincode=currentOrder.billingAddress.pincode
)
if len(previousBilling) > 0:
currentBillingId = currentOrder.billingAddress.id
currentOrder.billingAddress = previousBilling[0]
currentOrder.save()
BillingAddress.objects.get(id=currentBillingId).delete()
#The above code is not working
previousBilling=BillingAddress.objects.get(id=currentBillingId) previousBilling.delete()