Saving arbitrary data without database

103 views
Skip to first unread message

orestis

unread,
Oct 22, 2006, 8:21:23 PM10/22/06
to Django users
Hello,

I would like to save some arbitrary data that there's no point in
keeping in a database.

It's just a float number really, that some of my models will use, and
it will be updated using a cron job. It will not presented in a
template, just within some methods of a model.

What the heck, here's the full description:

I have defined a next() method that returns the next entry to be
updated. I want this to have a different behavior according to the
state of the data already present in the database. So I have made some
code that analyzes the current data and produces a threshold between 0
and 1, then I generate random numbers and decide what behavior to
choose depending random()>THRESHOLD.

The THRESHOLD will be calculated via a cron job - that's not a big
issue.
The issue is, how can I store the new value somewhere so everyone can
just import and use it ?

Instead of pickling and unpickling, I was wondering if Django has some
datastore for this kind of thing...

sago

unread,
Oct 23, 2006, 3:54:19 PM10/23/06
to Django users
I'm assuming you mean one single value, not one value per entry.

There are a whole bunch of ways you can store an item of data in
python. There's the pickle module, various flat-file database wrappers,
just write it to a file, etc.

But IMHO you are _much_ better off writing it to the database, even if
it is just a single value. You're better off because you're using only
a single database connection (for which you've already set your
properties), your application is portable, you don't have to worry
about locking problems (like you would if you just wrote out a file).

Just create a simple model with a single field (your number). And then
create a set method to set the one threshold in the database.

Ian

the....@gmail.com

unread,
Oct 24, 2006, 1:29:05 AM10/24/06
to Django users
use sqlite. its a file and works like a database, very lightweight.

spacedman

unread,
Oct 24, 2006, 5:19:09 AM10/24/06
to Django users
This sounds like you want to use my 'single row database' trickery
pokery:

http://groups.google.com/group/django-developers/browse_frm/thread/2caa976249783fae/#

Just create a Django model 'Properties' with your 'threshold' as one
field, then access it just like a normal Django model. The database
will only ever have one row, which is the single value you want.

Barry

Malcolm Tredinnick

unread,
Oct 24, 2006, 9:02:18 PM10/24/06
to django...@googlegroups.com

Alongside all the other suggestions you have, you can write your own
save() method on the model, as noted in the documentation, and write out
your attribute value to a file there.

Regards,
Malcolm

orestis

unread,
Oct 25, 2006, 4:41:46 AM10/25/06
to Django users
Thanks all for the recommendations!

Reply all
Reply to author
Forward
0 new messages