Django rest framework извлекает списки в сериализаторах

Вот модифицированная версия исходного кода (предоставленный код изменил значения по сравнению с моим исходным кодом ...)), который может создавать все эти фигуры, просто играя с параметрами:

Valley [/g1]

Roof [/g2]

Roof inversed [/g3]

Bridge [/g4]

Крыша не супер, но я оставлю ее вам, чтобы добавить поддержку масштабирования, поскольку это подразумевается в качестве примера.

ONLINE DEMO HERE

Инициализация:

var ctx = demo.getContext('2d'), /// context
    font = '64px impact',        /// font
    w = demo.width,              /// cache canvas width and height
    h = demo.height,
    curve,                       /// radius
    offsetY,                     /// offset for text
    bottom,                      /// bottom of text
    textHeight,                  /// text height (region of text)
    isTri = false,               /// is triangle shaped (roof)
    dltY,                        /// delta for triangle
    angleSteps = 180 / w,        /// angle steps for curved text
    i = w,                       /// "x" backwards
    y,                           /// top of text

    /// offscreen canvas that holds original text
    os = document.createElement('canvas'),
    octx = os.getContext('2d');

os.width = w;
os.height = h;

/// set font on off-screen canvas where we draw it
octx.font = font;
octx.textBaseline = 'top';
octx.textAlign = 'center';

Основная функция:

/// render
function renderBridgeText() {

    /// demo stuff snipped (see link)

    /// clear canvases    
    octx.clearRect(0, 0, w, h);
    ctx.clearRect(0, 0, w, h);

    /// draw text (text may change)
    octx.fillText(iText.value.toUpperCase(), w * 0.5, 0);

    /// slide and dice
    dltY = curve / textHeight;  /// calculate delta for roof/triangle
    y = 0;                      /// reset y in case we do roof
    i = w;                      /// init "x"

    while (i--) {

        if (isTri) {
            /// bounce delta value mid-way for triangle
            y += dltY;
            if (i === (w * 0.5)|0) dltY = -dltY;

        } else {
            /// calc length based on radius+angle for curve
            y = bottom - curve * Math.sin(i * angleSteps * Math.PI / 180);
        }

        /// draw a slice
        ctx.drawImage(os, i, 0, 1, textHeight,
                          i, h * 0.5 - offsetY / textHeight * y, 1, y);
    }
}

0
задан Sethen 16 January 2019 в 22:16
поделиться

1 ответ

class ArticleWithoutAuthorSerializer(ArticleSerializer):
    # Prevent author field serialization and endless nesting
    author = None

class AuthorSerializer(ModelSerializer):
    articles = SerializerMethodField()

    def get_articles(self, author):
        # Five recently posted articles
        articles = author.article_set.order_by('-published')[:5]
        return ArticleWithoutAuthorSerializer(articles, many=True).data

    class Meta:
        model = Author
0
ответ дан Alex K 16 January 2019 в 22:16
поделиться
Другие вопросы по тегам:

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