Giter Site home page Giter Site logo

django-admin-autoregister's Introduction

Django admin autoregister

Problem

Even though Django provides a very easy way to create admin views for your models, in 90% of situations you only need to display all the fields in the model with links between the related models. Retyping all the fields for all the models doesn't respect DRY principle.

Solution

One call to autoregister_admin() automatically creates and registers admin for all the models in the specified module with intelligent linking between ForeignKey, OneToOneField and ManyToManyField fields.

Installation

Because this is just a simple code snippet, it doesn't have an app. Instead copy the snippet to some module in your project. E.g.:

<your_project>/utils/autoregister.py

Usage

Supposing you have admin app correctly installed (see Admin installation). In your admin.py files add:

from . import models
from your_project.utils.autoregister import autoregister_admin
autoregister_admin(models)

And that's it! All the models in the module have admin views successfully created and registered.

Customization

exclude_models

To exclude some models and manully create admin for them, use optional exclude_models parameter:

autoregister(models, exclude_models=['ModelName1', 'ModelName2'])

model_fields

To display some additional fields for the models (e.g. some properties), use optional model_fields parameter:

autoregister(models, model_fields={'ModelName': ['property_1', 'property_2']})

exclude_fields

To exclude some fields from displaying, use optional exclude_fields parameter:

autoregister(models, exclude_fields={'ModelName': ['exclude_field1', 'exclude_field2']})

admin_fields

To do a little modifications to the generated admins (e.g. add search fields), use optional admin_fields parameter:

autoregister(models,
  admin_fields={
    'ModelName': {'search_fields': ['name'], 'list_filter': ['active']}
  }
)

reversed_relations

To include links to reversed relations of ForeignKey or Many2Many fields, use optional reversed_relations parameter:

# suppose we have the following models
class Model1(models.Model):
    pass

class Model2(models.Model):
    model1 = models.ForeignKey(Model1)  # this will be added automatically
#-------------------------------------------------------------------------

autoregister(models, reversed_relations={'Model1': ['model2']})

django-admin-autoregister's People

Contributors

mimino666 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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