Как выводить следующую запись на странице детального просмотра первой в Django?

Добрый день! Возникла проблема с реализацией фичи вывода предложенных статей на странице статьи. Использую Rest framework.
Так выглядит детальный вывод статьи json:
{
    "name": "Miss Ranepa",
    "w_type": "Branding",
    "brief": "Cras vitae orci non felis commodo.bibendum elementum vitae dui..Integer sed ex vehicula, vehicula.turpis sed, malesuada augue.",
    "idea": "Aliquam quis urna lobortis, hendrerit purus non,.consequat sem. Donec rutrum. In sit amet tortor ultrices, fringilla eros at, aliquet arcu.",
    "client": "Ranepa",
    "slider_preview_blurred": "http://127.0.0.1:3000/media/images/Miss_mtheme_blurred_1X7SIte.jpg",
    "articles": [
        {
            "tmp_url": "media/views/articles/single_img_view.html",
            "title": "first step",
            "images": [
                {
                    "img": "http://127.0.0.1:3000/media/images/slider_preview_first.jpg"
                }
            ],
            "paragraphs": []
        },
        {
            "tmp_url": "media/views/articles/multi_img_view.html",
            "title": "steps",
            "images": [
                {
                    "img": "http://127.0.0.1:3000/media/images/3ceb5b35687423.57001974a2e0a.png"
                },
                {
                    "img": "http://127.0.0.1:3000/media/images/82313035706461.57014dff4ce07.jpg"
                },
                {
                    "img": "http://127.0.0.1:3000/media/images/7a8ebd19136623.562d574a4ef04.png"
                }
            ],
            "paragraphs": []
        },
        {
            "tmp_url": "media/views/articles/text_m_img_view.html",
            "title": "text/image",
            "images": [
                {
                    "img": "http://127.0.0.1:3000/media/images/06be1435706461.570152577d509.jpg"
                },
                {
                    "img": "http://127.0.0.1:3000/media/images/56cc4a3203607.56349f7bd8575_18Kz1Jx.jpg"
                }
            ],
            "paragraphs": [
                {
                    "text": "Cras vitae orci non felis commodo.bibendum elementum vitae dui..Integer sed ex vehicula, vehicula.turpis sed, malesuada augue."
                },
                {
                    "text": "Aliquam quis urna lobortis, hendrerit purus non,.consequat sem. Donec rutrum. In sit amet tortor ultrices, fringilla eros at, aliquet arcu."
                }
            ]
        }
    ]
}

Нужно, чтобы после элементов articles выводилось также 2 случайных экземпляра case. Задача на первый взгляд несложная, но никак не могу сообразить как реализовать.

models.py:
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone

class Case(models.Model):
    name = models.CharField(_('case name'), max_length=30)
    OM_TYPES = (
        ('media/views/articles/single_img_view.html', 'single image'),
        ('media/views/articles/multi_img_view.html', '3 images'),
        ('media/views/articles/text_m_img_view.html', '2 images + text'),
        ('media/views/articles/text_img_view.html', 'text + image'),
        ('media/views/articles/video_view.html', 'video'),
        ('media/views/articles/carousel_view.html', 'carousel'),
    )
    case = models.ForeignKey(Case, related_name='articles')
    tmp_url = models.CharField(_('article type'), max_length=100, choices=OM_TYPES)
    title = models.CharField(_('article title'), max_length=99)

    def __str__(self):
        return self.title

class Image(models.Model):
    article = models.ForeignKey(Article, related_name='images') 
    img = models.ImageField(_('picture'), upload_to="images")

class Paragraph(models.Model):
    article = models.ForeignKey(Article, related_name='paragraphs')
    text = models.TextField(_('paragraph'), max_length=501)       

class Message(models.Model):
    email = models.EmailField(_('sender email'), max_length=42)
    msg = models.TextField(_('message'), max_length=200, default="undef")
    sent_at = models.DateTimeField(_('sent at'), auto_now_add=True)
     w_type = models.CharField(_('case type'), max_length=45)
    brief = models.TextField(_('brief description'), max_length=201)
    idea = models.TextField(_('case idea'), max_length=201)
    client = models.CharField(_('client'), max_length=45)
    preview = models.ImageField(_('preview image'), upload_to="images")
    created_at = models.DateTimeField(_('created at'), auto_now_add=True)
    slider_preview = models.ImageField(_('slider preview'), upload_to="images")
    slider_preview_blurred = models.ImageField(_('blurred slider preview'), upload_to="images")

    def __str__(self):
        return self.name

class Article(models.Model):


serializers.py:
from rest_framework import serializers
from landing.models import Case, Article, Image, Paragraph, Message

class CasePreviewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Case
        fields = [
            'id',
            'name',
            'w_type',
            'preview',
            'slider_preview',
            'url',
        ]

class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Image
        fields = ('img',)

class ParagraphSerializer(serializers.ModelSerializer):
    class Meta:
        model = Paragraph
        fields = ('text',)                

class ArticleSerializer(serializers.ModelSerializer):
    images = ImageSerializer(many=True, read_only=True)
    paragraphs = ParagraphSerializer(many=True, read_only=True)
    
    class Meta:
        model = Article
        fields = ('tmp_url', 'title', 'images', 'paragraphs')

class CaseDetailSerializer(serializers.ModelSerializer):
    articles = ArticleSerializer(many=True, read_only=True)
    suggested_case = CasePreviewSerializer(many=True, read_only=True)
    
    class Meta:
        model = Case
        fields = [
            'name',
            'w_type',
            'brief',
            'idea',
            'client',
            'slider_preview_blurred',
            'articles',
            'suggested_case',
        ]

class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = [
            'email',
            'msg',
            'sent_at',
        ]
  • Вопрос задан
  • 204 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы