Ram Rachum
Ram Rachum

Reputation: 88528

How about having a SingletonModel in Django?

I'm making a very simple website in Django. On one of the pages there is a vertical ticker box. I need to give the client a way to edit the contents of the ticker box as an HTMLField.

The first way that came to mind was to make a model Ticker which will have only one instance. Then I thought, instead of making sure manually that only one instance exists, perhaps there is (or there should be) something like a SingletonModel class in Django, which is like a normal model, except it makes sure no more than one instance gets created?

Or perhaps I should be solving my problem in a different way?

Upvotes: 21

Views: 12145

Answers (5)

Rabih Kodeih
Rabih Kodeih

Reputation: 9521

Try django-solo, it works in django 1.5 + for sure, django-singletons doesn't work with 1.5 + because it uses a deprecated feature.

Upvotes: 11

Lazybird
Lazybird

Reputation: 21

A model with only one instance, a singleton, is sometime useful for things like global settings that you want to edit from the admin instead of having them in Django settings.py.

There are several third party applications that helps implementing singleton models and improve the admin interface for instance, django-solo, django-singleton-admin, django-singletons.

Upvotes: 1

aabele
aabele

Reputation: 163

You can use django_singletons. It has a built in admin support.

Upvotes: 3

Carl Meyer
Carl Meyer

Reputation: 126541

I think having a "singleton" model is ugly; it's dumb use of the relational database and it's bad UI, because the admin UI is built around working with lists of objects.

Instead I prefer to use a generic solution like django-chunks or django-flatblocks for this.

Upvotes: 2

Brandon Henry
Brandon Henry

Reputation: 3720

rewrite your save method so that every time a Ticker object gets saved it overwrites the existing one (if one exists).

Upvotes: 1

Related Questions