'Non-database field in Django model

Is it possible to have a field in a Django model which does not get stored in the database.
For example:

class Book(models.Model):
    title = models.CharField(max_length=75)
    description models.CharField(max_length=255, blank=True)
    pages = models.IntegerField()
    none_db_field = ????

I could then do

book = Book.objects.get(pk=1)
book.none_db_field = 'some text...'
print book.none_db_field

Thanks



Solution 1:[1]

Creating a property on the model will do this, but you won't be able to query on it.

Example:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    def _get_full_name(self):
        return "%s %s" % (self.first_name, self.last_name)

    def _set_full_name(self, combined_name):
        self.first_name, self.last_name = combined_name.split(' ', 1)

    full_name = property(_get_full_name)

    full_name_2 = property(_get_full_name, _set_full_name)

Usage:

from mysite.models import Person

a = Person(first_name='John', last_name='Lennon')
a.save()
a.full_name
'John Lennon'

# The "full_name" property hasn't provided a "set" method.
a.full_name = 'Paul McCartney'
Traceback (most recent call last):
    ...
AttributeError: can't set attribute

# But "full_name_2" has, and it can be used to initialise the class.
a2 = Person(full_name_2 = 'Paul McCartney')
a2.save()
a2.first_name
'Paul'

Solution 2:[2]

To make it an instance variable (so each instance gets its own copy), you'll want to do this

class Book(models.Model):
    title = models.CharField(max_length=75)
    #etc

    def __init__(self, *args, **kwargs):
        super(Foo, self).__init__(*args, **kwargs)
        self.editable = False

Each Book will now have an editable that wont be persisted to the database

Solution 3:[3]

If you want i18n support:

# Created by [email protected] at 2022/5/2
from typing import Optional

from django.db import models
from django.utils.translation import gettext_lazy as _


class Blog(models.Model):
    title = models.CharField(max_length=128, unique=True, verbose_name=_("Title"))
    content = models.TextField(verbose_name=_("Content"))
    _visitors: Optional[int] = None

    @property
    def visitors(self):
        return self._visitors

    @visitors.setter
    def visitors(self, value):
        self._visitors = value

    visitors.fget.short_description = _("Visitors")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saturnix
Solution 2 The Mad Bug
Solution 3 BaiJiFeiLong