Answer the question
In order to leave comments, you need to log in
How to bind several images to one entity?
Good afternoon !
There is a model:
class ShopCat(models.Model):
title = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class ShopItem(models.Model):
cat_id = models.ForeignKey(ShopCat)
title = models.CharField(max_length=100)
sostav = models.TextField(max_length=1000)
descr = models.TextField(max_length=1000)
creation_date = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
price = models.IntegerField()
def __unicode__(self):
return self.title
class ShopItemImg(models.Model):
ShopItem_id = models.ForeignKey(ShopItem)
img = models.ImageField(upload_to='shop/', null=True, blank=True)
class ShopItemImgInline(admin.ModelAdmin):
model = ShopItemImg
extra = 3
class ShopItemAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['title', 'cat_id', 'price']}),
("Тексты", {'fields': ['descr', 'sostav']}),
]
admin.site.register(ShopItem, ShopItemAdmin)
admin.site.register(ShopCat)
admin.site.register(ShopItemImg, ShopItemImgInline)
Answer the question
In order to leave comments, you need to log in
I figured it out myself
The error was that I registered admin.site.register(ShopItemImg, ShopItemImgInline)
But I didn’t have to)
If anyone is interested, here is the working version of the admin panel:
# -*- coding: utf-8 -*-
from django.contrib import admin
from .models import ShopCat, ShopItem, ShopItemImg
class ShopItemImgInline(admin.TabularInline):
model = ShopItemImg
class ShopItemAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['title', 'cat_id', 'price']}),
("Тексты", {'fields': ['descr', 'sostav']}),
]
inlines = [ShopItemImgInline]
admin.site.register(ShopItem, ShopItemAdmin)
admin.site.register(ShopCat)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question