在Django開發一個單例模型怎么樣?

[英]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.

我在Django做一個非常簡單的網站。在其中一頁上有一個垂直的票務箱。我需要給客戶端一種方法來編輯作為HTMLField的ticker box的內容。

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?

首先想到的是制作一個只有一個實例的模型報價器。然后我想,與其手工確保只有一個實例存在,還不如在Django中有(或者應該有)一個類似SingletonModel類的類,這就像一個普通的模型,除非它確保創建的實例不超過一個?

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

或者我應該用另一種方式來解決我的問題?

5 个解决方案

#1


8  

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.

試試django-solo吧,它在django 1.5 +中是有效的,django-singletons沒有1.5 +,因為它使用的是不贊成的特性。

#2


3  

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

您可以使用django_singletons。它有內置的管理支持。

#3


2  

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.

我認為有一個“單例”模型是丑陋的;它是對關系數據庫的愚蠢使用,它是糟糕的UI,因為管理UI是建立在處理對象列表的基礎上的。

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

相反,我更喜歡使用像django chunk或django flatblocks這樣的通用解決方案。

#4


1  

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

重寫保存方法,以便每次保存Ticker對象時都覆蓋現有的方法(如果存在的話)。

#5


1  

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.

只有一個實例(單例)的模型有時對於全局設置(您希望從admin中編輯它們,而不是在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.

有幾個第三方應用程序可以幫助實現單例模型並改進管理界面,例如,django-solo、django-singleton-admin、django-singletons。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/16/720807b4ac96a392d5c97f1b0811e8f9.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com