3π
β
What you can use here is Admin inlines that helps you easily edit/updated related items.
class ProductImagesInline(admin.StackedInline):
model = Images
class ProductAdmin(admin.ModelAdmin):
list_display = ['name', 'slug', 'category', 'price', 'stock', 'available', 'created', 'updated']
list_filter = ['available', 'created', 'updated', 'category']
list_editable = ['price', 'stock', 'available']
prepopulated_fields = {'slug': ('name',)}
inlines = [ProductImagesInline]
Once images you can access them like product.images.all()
π€Bipul Jain
1π
Use one of the Inline Admins for the Image
(yes, you should give it a singular name!) model:
class ImageInline(admin.TabularInline):
model = Image
class ProductAdmin(admin.ModelAdmin):
...
inlines = [
ImageInline,
]
For properly displaying images in templates, you should consider a thumbnail library like sorl or easy-thumbnails:
# sorl example
{% for image in product.images.all %}
{% thumbnail image.image "500x500" format="PNG" upscale=False as thumb %}
# ^^^^^ you need the image field here
<img src="{{ thumb.url }}" alt="{{ product.name }}">
{% endthumbnail %}
{% endfor %}
Beyond that, you can access an Image
βs url via image.image.url
. This is where your original image file will be served. This requires you to have MEDIA_ROOT
and MEDIA_URL
properly configured.
π€user2390182
- [Django]-Django admin custom commands β passing a list of strings into args
- [Django]-Django β get user id after saving the user
- [Django]-Sending email from webserver β django python
- [Django]-Django project on 80 port?
1π
models.py
class Product(models.Model):
''''fields''''
class Image(models.Model):
image = models.ImageField()
product = models.ForeignKey(Product, default=None, related_name='images',on_delete=models.PROTECT)
admin.py
from yourapp.models import Product, Images
class InlineImage(admin.TabularInline):
model = Images
class ProductAdmin(admin.ModelAdmin):
inlines = [InlineImage]
admin.site.register(Product, ProductAdmin)
π€Cadmus
- [Django]-Error in PIL installed with PIP β Django,Python
- [Django]-DRF APIView β How can I add help text to auto generated form?
- [Django]-How would you write an `is_pdf(path_to_file)` function in Python?
- [Django]-Using UUIDField in Django keeps creating new migrations
- [Django]-TypeError: unhashable type: 'dict' Django
Source:stackexchange.com