Объект 'Account' не имеет атрибута 'orderproduct' Загрузка и предварительный просмотр не отображаются
Мне нужна ситуация, когда пользователь покупает продукт, и создается orderproduct, он может скачать изображение...
Это продукт Модель
class Product(models.Model):
# user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# managers = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="product_manager")
seller = models.ForeignKey(SellerAccount, on_delete=models.CASCADE)
media = models.ImageField(blank=True, null=True, upload_to=download_media_location, storage = FileSystemStorage(location=(protected_uploads_loc)))
title = models.CharField(max_length=250)
slug = models.SlugField(blank=True, unique=True)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=100, default=15.99)
sale_active = models.BooleanField(default=False)
sales_price = models.DecimalField(decimal_places=2, max_digits=100, default=9.99, null=True,
blank=True)
image_size = models.CharField(max_length=20, null=True)
# date_taken = models.DateTimeField(auto_now_add=True, auto_now=False)
location = models.CharField(max_length=150, null=True)
Это модель "заказ-продукт"
class OrderProduct(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank=True, null=True)
user = models.ForeignKey(Account, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
# download = models.ManyToManyField(Product, related_name="download_product")
ordered = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.username
Виды загрузки продукта
class ProductDownloadView(MultiSlugMixin, DetailView):
model = Product
def get(self, request, *args, **kwargs):
obj = self.get_object()
# if obj in request.user.myproducts.products.all():
if obj in request.user.orderproduct.product_set.all():
filepath = Path.joinpath(settings.PROTECTED_UPLOADS, obj.media.path)
print(filepath)
guessed_type = guess_type(filepath)[0]
wrapper = FileWrapper(open(filepath, "rb"))
mimetype = "application/force-download"
if guessed_type:
mimetype = guessed_type
response = HttpResponse(wrapper, content_type=mimetype)
if not request.GET.get("preview"):
response["Content-Disposition"] = "attachment; file=%s"%(obj.media.name)
response['X-SendFile'] = str(obj.media.name)
return response
else:
raise Http404
Это html-шаблон
{% if request.user.is_authenticated and object.media and object in request.user.orderproduct.product_set.all %}
<p>
<a href="{{ object.get_download }}">Download</a>
<a href="{{ object.get_download }}?preview=True">Preview</a>
</p>
{% else %}
<a class="btn btn-warning btn-xl js-scroll-trigger p-3"
href="{% url 'cart:add_cart' product.id %}">ADD TO CART</a>
{% endif %}