Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-database field in Django model

Tags:

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

like image 394
John Avatar asked Feb 12 '10 09:02

John


1 Answers

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 
like image 81
Jack M. Avatar answered Oct 07 '22 17:10

Jack M.