R
R
Rodion Yurchenko2016-05-27 12:12:55
Django
Rodion Yurchenko, 2016-05-27 12:12:55

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)

And in admin.py I write:
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)

Everything works fine, BUT
When trying to change ModelAdmin to StackedInline or TabularInline in the ShopItemImgInline(admin.ModelAdmin) admin class, it gives an error: AttributeError: 'ShopItemImgInline' object has no attribute 'urls'
Tell me - where did you mess up?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rodion Yurchenko, 2016-05-27
@aassdds

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 question

Ask a Question

731 491 924 answers to any question