So I discovered if you override 'save' and 'delete' in your model with:
def save(self):
self.id=1
super(mySettings, self).save()
def delete(self):
pass
then if you add a new row it just replaces the id=1 row, and if you
delete a row, nothing happens. If you create the row using the initial
SQL feature then you can be (almost) sure that there's only ever one
row in the table. Then when your view code needs to get a value, it
just queries the mySettings table and gets it.
Seems kinda neat, but obviously some people may be confused when 'add
mySetttings' doesnt actually add anything, and 'delete' doesnt actually
delete it. Would there be enough demand for this kinda thing to make it
a separate class of model, such that you could do:
class mySettings(models.Properties):
and then tweak the admin screens slightly to compensate for the
different behaviour?
Barry
--
Ned Batchelder, http://nedbatchelder.com
The problem with the (name,value) approach is that all the values have
to be the same type, so you cant present some properties as text, some
as boolean, and some as drop-down choices. Also you might want to
constrain the set of allowed names, and not allow additions.
You are right: the name, value method does restrict you to only one
value type.
class Property(models.Model):
class Admin:
pass
showGone = models.BooleanField(default=False)
goneGroup = models.CharField(maxlength=100)
def save(self):
""" save, but set id to 1 """
self.id=1
super(Property, self).save()
def delete(self):
pass
def __str__(self):
return "Properties: "+" Show gone: "+str(self.showGone)+", Gone
Group: "+str(self.goneGroup)
My motivation here was to have a group of users that I can either show
or not show in a listing. The properties here control whether that flag
is set or not and the name of the group. Its almost a proof-of-concept
but there are plenty of other examples.