25๐
โ
As long as you do not want the property to persist, I donโt see why you canโt create a property like you described. I actually do the same thing on certain models to determine which are editable.
class Email(EntryObj):
ts = models.DateTimeField(auto_now_add=True)
body = models.TextField(blank=True)
user = models.ForeignKey(User, blank=True, null=True)
editable = False
...
class Note(EntryObj):
ts = models.DateTimeField(auto_now_add=True)
note = models.TextField(blank=True)
user = models.ForeignKey(User, blank=True, null=True)
editable = True
๐คJack M.
16๐
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'
- [Django]-Django: signal when user logs in?
- [Django]-Is this the right way to do dependency injection in Django?
- [Django]-How do you include a csrf token when testing a POST endpoint in django?
7๐
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
๐คThe Mad Bug
- [Django]-How to validate an Email address in Django?
- [Django]-Users in initial data fixture
- [Django]-Accessing Primary Key from URL in Django View Class
1๐
If you want i18n support:
# Created by BaiJiFeiLong@gmail.com 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")
๐คBaiJiFeiLong
- [Django]-Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin
- [Django]-CherryPy vs Django
- [Django]-Unable to connect to server: PgAdmin 4
Source:stackexchange.com