В настоящее время я реплицирую веб-сайт электронной коммерции в django, следуя бесплатному курсу с YouTube: https://www.youtube.com/playlist?list=PLPp4GCMxKSjCM9AvhmF9OHyyaJsN8rsZK, при загрузке файлов изображений через страницу администратора изображения перезаписываются вместо сохранения нескольких изображения для того же продукта, ссылка на мой репозиторий github: https://github.com/mddawood/Ecommerce.git и мой соответствующий код приведен ниже:
models.py
from django.urls import reverse
from django.db import models
class Product(models.Model):
title = models.CharField(max_length = 120)
description = models.TextField(null = True, blank = True)
price = models.DecimalField(decimal_places = 2, max_digits = 100, default = 29.99)
sale_price = models.DecimalField(decimal_places = 2, max_digits = 100, null = True, blank = True)
slug = models.SlugField(unique = True)
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
active = models.BooleanField(default = True)
def __str__(self):
return self.title
class Meta:
unique_together = ('title', 'slug')
def get_price(self):
return self.price
def get_absolute_url(self):
return reverse("single_product", kwargs = {"slug": self.slug})
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete = models.CASCADE)
featured = models.BooleanField(default = False)
image = models.ImageField(upload_to = 'images/')
thumbnail = models.BooleanField(default = False)
active = models.BooleanField(default = True)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
def get_absolute_image_url(self):
return os.path.join(settings.MEDIA_URL, self.images.url)
def __str__(self):
return self.product.title
admin. py
from django.contrib import admin
# Register your models here.
from .models import Product, ProductImage
class ProductAdmin(admin.ModelAdmin):
date_hierarchy = 'timestamp'
search_fields = ['title', 'description']
list_display = ['title', 'price', 'active', 'updated']
list_editable = ['price', 'active']
list_filter = ['price', 'active']
readonly_fields = ['updated', 'timestamp']
prepopulated_fields = {"slug": ("title",)}
class Meta:
model = Product
admin.site.register(Product, ProductAdmin)
admin.site.register(ProductImage)