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...
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
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
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