Answer the question
In order to leave comments, you need to log in
What is wrong in model or admin links?
Good day!
Lack of knowledge and search has not helped yet.
I write my bike photo gallery.
Challenge: There is a ride that has multiple photos.
In the admin panel I create a product. In the upper part of the product data, in the lower associated table for downloading pictures.
It gives me an error
: (admin.E202) 'photolog.Images' has no ForeignKey to 'photolog.PhotoObject'
# models.py
class Images(models.Model):
img = models.ImageField(upload_to='photolog', verbose_name='Изображение')
class Meta:
verbose_name = 'Изображение'
verbose_name_plural = 'Изображения'
def thumb(self):
if self.img:
path = self.img.url
return format_html('<img src="{}" height="60">'.format(path))
else:
return '(ПУСТО)'
class PhotoObject(models.Model):
'Фотообъект - объект, у которого больше одной фотографии'
imgs = models.ForeignKey(Images, blank=True, null=True, verbose_name='Картинки', on_delete=models.CASCADE)
title = models.CharField(max_length=200, verbose_name='Заголовок')
slug = models.SlugField(max_length=200, verbose_name='url статьи')
description = models.CharField(max_length=512, blank=True, null=True, verbose_name='Краткое описание')
keywords = models.CharField(max_length=255, blank=True, null=True, verbose_name='Ключевые слова')
class Meta:
verbose_name = 'Фотообъект'
verbose_name_plural = 'Фотообъекты'
def __str__(self):
return self.title
# admin.py
class ImagesAdmin(admin.TabularInline):
model = Images
extra = 1
fields = ( 'img', 'thumb', )
readonly_fields = ('thumb',)
@admin.register(PhotoObject)
class PhotoObjectAdmin(admin.ModelAdmin):
list_display = ('title', 'slug')
fieldsets = [
(None,
{'fields': (('title', 'slug'), 'keywords', 'description')}),
]
prepopulated_fields = {'slug': ('title',)}
inlines = [ImagesAdmin]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question