Skip to main content

Choices of select widget changes while the value of another filed changes.

Project description

django-gazing-select-widget

Choices of select widget changes while the value of another filed changes.

Install

pip install django-gazing-select-widget

DjangoGazingSelectWidget Init Parameters

class DjangoGazingSelectWidget(Select):

    def __init__(self, gazing_field, choices_related, empty_label="-"*10, attrs=None, choices=()):
        ...
  • gazing_field: Name of the gazing field.
  • choices_related: List of tuple (option_value, related_value)
    • It means, if the gazing_field value equals to related_value, then the option with option_value will be show.
  • empty_label: Defualt to "-"*10. Empty value option for this field.
  • hide_all_if_empty: Default to true. If true, hide all options if gazing value is empty. If false, show all options if gazing value is empty.
  • attrs: Same with system's default Select widget.
  • choices: Same with system's default Select widget.

Usage

**pro/settings.py

INSTALLED_APPS = [
    ...
    'django_static_jquery3',
    'django_gazing_select_widget',
    ...
]

app/admin.py

from django.contrib import admin
from django import forms
from django_gazing_select_widget.widgets import DjangoGazingSelectWidget
from .models import Category

cat1_choices = [
    ("", "-"*10),
    (1, "1"),
    (2, "2"),
]

cat2_choices = [
    ("group1", [
        (1, "a1"),
        (2, "b1"),
        (3, "c1"),
        (4, "d1"),
    ]),
    ("group2", [
        (5, "a2"),
        (6, "b2"),
        (7, "c2"),
        (8, "d2"),
    ]),
]

cat2_choices_related = {
    1: ["1"],
    2: ["1"],
    3: ["1"],
    4: ["1"],
    5: ["2"],
    6: ["2"],
    7: ["2"],
    8: ["2"],
}

class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = []
        widgets = {
            "cat1": forms.Select(choices=cat1_choices),
            "cat2": DjangoGazingSelectWidget(gazing_field="cat1", choices=cat2_choices, choices_related=cat2_choices_related, hide_all_if_empty=False),
        }

class CategoryAdmin(admin.ModelAdmin):
    list_display = ["cat1", "cat2", "name"]
    form = CategoryForm

admin.site.register(Category, CategoryAdmin)

Note:

  1. field cat2 is gazing at field cat1.
  2. if the field cat1's value is empty, show none options in field cat2.
  3. if the field cat1's value is 1, show group1 options in field cat2.
  4. if the field cat1's value is 2, show group2 options in field cat2.

Releases

v0.1.0 2020/03/28

  • First release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-gazing-select-widget-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file django-gazing-select-widget-0.1.0.tar.gz.

File metadata

  • Download URL: django-gazing-select-widget-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for django-gazing-select-widget-0.1.0.tar.gz
Algorithm Hash digest
SHA256 29ef4abbdf03f817e6959a19900eec4b837fad896cc05f936ebe1fa57e4fe095
MD5 32120dc482412c33f7a949b948ba8219
BLAKE2b-256 e6df1d7157596bdfdfdadf4c8593acc010c7e7112ccc16750905d2862703da35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page