Giter Club home page Giter Club logo

django-singletons's Introduction

I keep finding myself re-using this simple bit of code, so I thought I should open-source it, even though it's not much more than a snippet.

A SingletonModel is a django model that only ever has one record. You can't use the admin to create a new instance, or delete the existing one.

Some might argue that singleton models are an inefficient way of using a relational database, but in practice it's no biggie - most web sites have some important one-off content (eg. the Home Page), and  singleton models map well to how content editors generally think.

I suspect I have cadged some of this code from someone else (likely ex-colleague http://github.com/jphalip/), but a quick Google doesn't show up anything like this already out there. So here it is. I hope you find it useful.

Installation
------------

pip install django-singletons

To get the custom admin templates working, you need to add "singleton_models" to your INSTALLED_APPS

Example Usage
-------------

in models.py


from singleton_models.models import SingletonModel

class HomePage(SingletonModel):
    welcome = models.TextField()
    
    def __unicode__(self):
        return u"The Home Page" # something like this will make admin message strings more coherent
        
    class Meta:
        verbose_name = "Home Page" # once again this will make sure your admin UI doesn't have illogical text
        verbose_name_plural = "Home Page"


in admin.py

from singleton_models.admin import SingletonModelAdmin
from models import HomePage
        
admin.site.register(HomePage, SingletonModelAdmin)

django-singletons's People

Contributors

askeyt avatar cchurch avatar olivergeorge avatar tttallis avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.