GithubHelp home page GithubHelp logo

niranjrajasekaran / django-smart-selects Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jazzband/django-smart-selects

0.0 2.0 0.0 370 KB

chained and grouped selects for django forms

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-smart-selects's Introduction

Django Smart Selects

Chained Selects

If you have the following model:

class Location(models.Model)
	continent = models.ForeignKey(Continent)
	country = models.ForeignKey(Country)
	area = models.ForeignKey(Area)
	city = models.CharField(max_length=50)
	street = models.CharField(max_length=100)

And you want that if you select a continent only the countries are available that are located on this continent and the same for areas you can do the following:

from smart_selects.db_fields import ChainedForeignKey 

class Location(models.Model)
	continent = models.ForeignKey(Continent)
	country = ChainedForeignKey(
		Country, 
		chained_field="continent",
		chained_model_field="continent", 
		show_all=False, 
		auto_choose=True
	)
	area = ChainedForeignKey(Area, chained_field="country", chained_model_field="country")
	city = models.CharField(max_length=50)
	street = models.CharField(max_length=100)

This example asumes that the Country Model has a continent = ForeignKey(Continent) field and that the Area model has country = ForeignKey(Country) field.

  • The chained field is the field on the same model the field should be chained too.
  • The chained model field is the field of the chained model that corresponds to the model linked too by the chained field.
  • show_all indicates if only the filtered results should be shown or if you also want to display the other results further down.
  • auto_choose indicates that if there is only one option if it should be autoselected.

Grouped Selects

If you have the following model:

class Location(models.Model)
	continent = models.ForeignKey(Continent)
	country = models.ForeignKey(Country)

And you want that all countries are grouped by the Continent and that Groups are used in the select change to the following:

from smart_selects.db_fields import GroupedForeignKey

class Location(models.Model)
	continent = models.ForeignKey(Continent)
	country = GroupedForeignKey(Country, "continent")

This example assumes that the Country Model has a foreignKey to Continent named "continent" finished.

Installation

  1. Add "smart_selects" to your INSTALLED_APPS
  2. Bind the smart_selects urls.py into your main urls.py with something like: url(r'^chaining/', include('smart_selects.urls')), This is needed for the chained-selects.
  3. Profit

Settings

USE_DJANGO_JQUERY : By default, smart_selects will use the bundled jQuery from Django 1.2's admin area. Set USE_DJANGO_JQUERY = False to disable this behaviour.

JQUERY_URL : By default, jQuery will be loaded from Google's CDN. If you would prefer to use a different version put the full URL here.

django-smart-selects's People

Contributors

digi604 avatar joelcrocker avatar goinnn avatar gearheart avatar acdha avatar petrdlouhy avatar sean-wallace avatar viniciuscainelli avatar litchfield avatar russelldavies avatar mkalinowski avatar ionelmc avatar fabianbuechler avatar dmoisset avatar andreisavu avatar atereshkin avatar alekam avatar

Watchers

James Cloos avatar Niranj Rajasekaran 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.