I am trying to copy image from my ProductImage models and upload it to my OrderItem models when any new order items will be create but got this error AttributeError: 'NoneType' object has no attribute 'image'
but you can see my ProductImage model have image fields and also image exits for the current product which order is creating. here my two models:
#ProductImage Model
class ProductImage(models.Model):
product = models.ForeignKey(Product,on_delete=models.CASCADE,blank=True,null=True)
image = models.ImageField(upload_to=dynamic_upload_path)
thumb_image = models.ImageField(upload_to=dynamic_upload_path,blank=True,null=True)
#OrderItem models
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE,blank=True,null=True)
product = models.ForeignKey(Product, on_delete=models.SET_NULL,blank=True,null=True)
product_image = models.ForeignKey(ProductImage, on_delete=models.SET_NULL,blank=True,null=True)
order_image_file = models.ImageField(blank=True,null=True,upload_to=orderImage_upload_path)
my signals:
@receiver(post_save, sender=OrderItem)
def OrderItemSignals(sender, instance, created,**kwargs):
if created:
instance.order_image_file = instance.product_image.image
so basically when any order item will be create I want to fire my post save for copy image from my product image models and upload it to my order item models image fields.