How to define non-persisted attributes in Model

507 views
Skip to first unread message

shevken

unread,
Mar 10, 2007, 9:45:09 AM3/10/07
to Django users
class Message(models.Model):
description = models.CharField(maxlength=200)
author = models.ForeignKey(User)
temp = models.CharField(maxlength=100)


When i syncdb, i do not want the column temp to be created. Is there
any arguments i can pass to enable this?

Eg.

temp = models.CharField(maxlength=100, persist=False)

anders conbere

unread,
Mar 10, 2007, 10:51:15 AM3/10/07
to django...@googlegroups.com
I would just not make it a model.* since you don't want that value to
be part of the Database, just don't use the ORM for it.

~ Anders

Kenny Lee

unread,
Mar 10, 2007, 11:46:33 AM3/10/07
to django...@googlegroups.com
I need a model as i will need to save to the database. Just that i need to serialize the object to send it to json, and it will be helpful to have some custom attributes for temporary processing, but will not be saved to the db.

anders conbere

unread,
Mar 10, 2007, 1:58:56 PM3/10/07
to django...@googlegroups.com
well like I said you can either make that particular field not a model
object, or you could just add the attributes at run time. I'm not
familiar with using the json serializer so I'm not sure about the
specifics there, but for simply adding attributes to the model that
wouldn't be saved to the db, both of those options are viable.

~ Anders

Horst Gutmann

unread,
Mar 11, 2007, 5:37:26 AM3/11/07
to django...@googlegroups.com
Do you really need a CharField here or would a simply string variable be
enough? So you could simply do something like

class Message(models.Model):
description = models.CharField(maxlength=200)
author = models.ForeignKey(User)
temp = ""

and it should never get stored in the database as far as I know... Or do
you need to have a CharField for a specific reason?

anders conbere schrieb:

Reply all
Reply to author
Forward
0 new messages