A
A
alex5e2018-02-10 11:53:23
Django
alex5e, 2018-02-10 11:53:23

How to create a model attribute in Django without adding a field to the database?

I have an Article model defined in models.py

class Article(models.Model):
    title = models.CharField(max_length=256)
    slug = models.SlugField(max_length=256)
    short_content = RichTextField(config_name='awesome_ckeditor')
    full_content = RichTextField(config_name='awesome_ckeditor')
    cover = models.ImageField(blank=True, upload_to='uploaded_images')
    cropping = ImageRatioField('cover', '200x200')
    show = models.BooleanField(default=0)
    visits = models.BooleanField(default=0)

the makemigrations command will generate sql for me to create a table in the database with acc. fields
How can I make it so that the cropping attribute in the model is (needed to create image thumbnails in the admin panel), but at the same time a separate field in the database is not created for it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neatsoft, 2018-02-10
@alex5e

You can use either property for this :

import os

from django.db import models


class Article(models.Model):
    title = models.CharField(
        max_length=200,
    )
    image = models.ImageField(
        blank=True,
        upload_to='images',
    )

    @property
    def thumbnail(self):
        if self.image:
            path, filename = os.path.split(self.image.url)
            name, ext = os.path.splitext(filename)
            filename = 'prefix_' + name + '_suffix' + ext
            return os.path.join(path, filename)

or a function:
import os

from django.contrib import admin
from django.utils.html import format_html


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = (
        'title',
        'get_thumbnail',
    )
    fields = (
        'title',
        'get_thumbnail',
    )
    readonly_fields = (
        'get_thumbnail',
    )

    def get_thumbnail(self, obj):
        if obj.image:
            path, filename = os.path.split(self.image.url)
            name, ext = os.path.splitext(filename)
            filename = 'prefix_' + name + '_suffix' + ext
            return format_html('<img src="{}">', os.path.join(path, filename))
    get_thumbnail.short_description = 'Thumbnail'
    get_logo.admin_order_field = 'image'

or both:
import os

from django.contrib import admin
from django.utils.html import format_html


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = (
        'title',
        'get_thumbnail',
    )
    fields = (
        'title',
        'get_thumbnail',
    )
    readonly_fields = (
        'get_thumbnail',
    )

    def get_thumbnail(self, obj):
        url = obj.thumbnail
        return format_html('<img src="{}">', url) if url else ''
    get_thumbnail.short_description = 'Thumbnail'
    get_logo.admin_order_field = 'image'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question