无法使用 Django 信号创建卖家实例
Failed to create a seller instance using django signals
我正在构建一个有 2 种用户类型的网站,并且对 django 还是个新手。
而且我想添加功能以在销售产品时添加产品的卖家。
对不起,我无法更好地解释它。
这是models.py的代码:
class Ordered(models.Model):
products = models.ForeignKey(Products, on_delete = models.SET_NULL, null = True)
seller = models.ForeignKey(SellerProfile, on_delete = models.SET_NULL, null = True)
buyer = models.ForeignKey(CustomerProfile, on_delete = models.CASCADE)
ordered_on = models.DateTimeField(auto_now_add = True)
product/models.py
class Products(models.Model):
seller = models.ForeignKey(SellerProfile, on_delete = models.CASCADE)
title = models.CharField(max_length = 255)
product_category = models.CharField(choices = CATEGORY_CHOICES, max_length = 100, default = 'eBooks')
description = models.TextField()
files = models.FileField(upload_to = 'media/product_files/', null = True)
slug = models.SlugField(max_length = 255, unique = True, null = True, blank = True)
这是信号代码:
@receiver(post_save, sender = Ordered)
def new_order_for_seller(sender, instance, created, *args, **kwargs):
seller = Ordered.seller.sellerprofile
if created:
Ordered.objects.create(seller = seller)
对代码的任何建议或更正都会非常有帮助。
谢谢
您可以将 seller
属性设置为 instance.product.seller
:
@receiver(pre_save, sender = Ordered)
def new_order_for_seller(sender, instance, created, *args, **kwargs):
if created and instance.product is not None:
instance<strong>.seller_id</strong> = instance<strong>.product.seller_id</strong>
我们可以在 pre_save
信号中执行此操作,以防止再次保存新的 Ordered
对象。
也就是说,由于卖家已经由 Product
确定,因此复制它没有多大意义,因为它最终会导致 Seller
的不一致 Product
稍后更改,Ordered
仍指向“旧”Seller
。
我正在构建一个有 2 种用户类型的网站,并且对 django 还是个新手。 而且我想添加功能以在销售产品时添加产品的卖家。 对不起,我无法更好地解释它。 这是models.py的代码:
class Ordered(models.Model):
products = models.ForeignKey(Products, on_delete = models.SET_NULL, null = True)
seller = models.ForeignKey(SellerProfile, on_delete = models.SET_NULL, null = True)
buyer = models.ForeignKey(CustomerProfile, on_delete = models.CASCADE)
ordered_on = models.DateTimeField(auto_now_add = True)
product/models.py
class Products(models.Model):
seller = models.ForeignKey(SellerProfile, on_delete = models.CASCADE)
title = models.CharField(max_length = 255)
product_category = models.CharField(choices = CATEGORY_CHOICES, max_length = 100, default = 'eBooks')
description = models.TextField()
files = models.FileField(upload_to = 'media/product_files/', null = True)
slug = models.SlugField(max_length = 255, unique = True, null = True, blank = True)
这是信号代码:
@receiver(post_save, sender = Ordered)
def new_order_for_seller(sender, instance, created, *args, **kwargs):
seller = Ordered.seller.sellerprofile
if created:
Ordered.objects.create(seller = seller)
对代码的任何建议或更正都会非常有帮助。 谢谢
您可以将 seller
属性设置为 instance.product.seller
:
@receiver(pre_save, sender = Ordered)
def new_order_for_seller(sender, instance, created, *args, **kwargs):
if created and instance.product is not None:
instance<strong>.seller_id</strong> = instance<strong>.product.seller_id</strong>
我们可以在 pre_save
信号中执行此操作,以防止再次保存新的 Ordered
对象。
也就是说,由于卖家已经由 Product
确定,因此复制它没有多大意义,因为它最终会导致 Seller
的不一致 Product
稍后更改,Ordered
仍指向“旧”Seller
。