A
A
Alexander Vinogradov2019-01-09 08:37:37
Django
Alexander Vinogradov, 2019-01-09 08:37:37

How to filter related model list by another field?

Good day!
I have a Core app with a StandardModel that is linked to another app's model by the phobj field.
In the admin panel, when adding a StandardModel object, the connection to the PhotoObject is selected from the list (see picture).
The PhotoObject has a parent Catalog object.
Question: how to filter the PhotoObject list by the Catalog model in the admin panel?
And the second: how to display pictures in the list?
______________
Thanks!

Core
# models.py

class StandardModel(models.Model):
    'Базовые шкафы'
    
    title = models.CharField(max_length=200, verbose_name='Название')
    article = models.CharField(max_length=10, unique=True, verbose_name='Артикул')
    slug = models.SlugField(max_length=50, verbose_name='url модели')
    qdoors = models.SmallIntegerField(choices=QUANTITY_DOORS, default=2, verbose_name='Количество дверей')
    type_case = models.CharField(max_length=10, choices=TYPE_CASE, default='case', verbose_name='Тип корпуса')
    angular = models.BooleanField(default=False, verbose_name='Угловой')
    description = models.TextField(blank=True, null=True, verbose_name='Описание')
    height1 = models.SmallIntegerField(default=2200, verbose_name='Высота от')
    height2 = models.SmallIntegerField(default=2400, verbose_name='Высота до')
    width1 = models.SmallIntegerField(default=1200, verbose_name='Ширина от')
    width2 = models.SmallIntegerField(default=2000, verbose_name='Ширина до')
    depth = models.SmallIntegerField(default=600, verbose_name='Глубина')
    phobj = models.ForeignKey(PhotoObject, blank=True, null=True, on_delete=models.SET_NULL, verbose_name='Фотообъект')

# admin.py

@admin.register(models.StandardModel)
class StandartCasesAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Данные', {
            'fields': ('title', ('article', 'slug'), 'description')
            }),
        ('Конструктив', {
            'fields': (('qdoors', 'type_case', 'angular'),)
            }),
        ('Размеры', {
            'fields': (('height1', 'height2'),
                       ('width1', 'width2'),
                       'depth', ('thumb', 'phobj'))
            })
        )
    list_display = ('thumb', 'article', 'title', 'qdoors', 'type_case', 'angular')
    search_fields = ('title', 'article', 'qdoors', 'type_case', 'angular')
    list_filter = ('qdoors', 'type_case', 'angular')
    readonly_fields = ('thumb', )
    prepopulated_fields = {'slug': ('article',)}

5c35870acb42c661256943.jpeg
photolog
# models.py

class Catalog(models.Model):

    title = models.CharField(max_length=200, verbose_name='Название')    

class PhotoObject(models.Model):
    'Фотообъект - объект, у которого больше одной фотографии'

    catalog = models.ForeignKey(Catalog, blank=True, null=True, on_delete=models.CASCADE)
    title = models.CharField(max_length=200, verbose_name='Название')
    description = models.TextField(max_length=512, blank=True, null=True, verbose_name='Описание')
    keywords = models.CharField(max_length=255, blank=True, null=True, verbose_name='Ключевые слова')
    pub_date = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')

 class Images(models.Model):

    img = models.ImageField(storage=OverwriteStorage(), upload_to='photolog', verbose_name='Изображение')
    phobj = models.ForeignKey(PhotoObject, blank=True, null=True, on_delete=models.CASCADE)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-01-09
@sergey-gornostaev

How can I filter the list in the admin for ForeignKey?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question