Skip to main content

We only care about year and month for DateField, and always set the day to 1, it's a Django Widget allow you select the year and month. And now we support CharField backend.

Project description

django-yearmonth-widget

We only care about year and month for DateField, and always set the day to 1, it's a Django Widget allow you select the year and month. And now we support CharField backend.

Install

pip install django-yearmonth-widget

Usage

pro/settings.py

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

app/admin.py

from django.contrib import admin
from django import forms
from django_yearmonth_widget.widgets import DjangoYearMonthWidget
from .models import Book


class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        exclude = []
        widgets = {
            "published_yearmonth": DjangoYearMonthWidget(),
        }

class BookAdmin(admin.ModelAdmin):
    form = BookForm
    list_display = ["name", "published_time"]

admin.site.register(Book, BookAdmin)

Note:

  1. Create a ModelForm, and set new widget for the field.

DjangoYearMonthWidget Init parameters

  • years: list of year numbers. Default to None.
    • If years provided, parameters prev_years and next_years are ignored.
  • start_year: Default to None. Use to set the beginning of year range, have higher priority than prev_years.
  • end_year: Default to None. Use to set the ending of year range, have higher priority than next_years.
  • prev_years: int, default to 10.
  • next_years: int, default to 0.
    • Use prev_years, next_years to set the year range based on today's year.
    • prev_years means the begining year is now.year - prev_years. If today's year is 2020 and the prev_years=10, so that the final years is start at 2010.
    • next_years means the ending year is now.year + next_years. If today's year is 2020 and the next_years=10, so that the final years is end at 2030.
  • day_value: int, default to 1.
  • value_dumps: string template, e.g. %(year)04d-%(month)02d-%(day)02d
  • value_loads: regex parse the string value, e.g. (?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})
    • value_dumps and value_loads must matchs with each other, it means value_loads regex CAN parse string dumped with value_dumps.
    • value_dumps MUST in %(xx)d so that both python and javascript can do the string formatting.

Releases

v0.2.0 2020/03/16

  • Add start_year & end_year parameters. The parameters start_year and end_year have higher priority than prev_years and next_years.
  • Add support for CharField. You must provide value_dumps and value_loads for CharField backend.

v0.1.0 2020/03/10

  • 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-yearmonth-widget-0.2.0.tar.gz (8.0 kB view details)

Uploaded Source

File details

Details for the file django-yearmonth-widget-0.2.0.tar.gz.

File metadata

  • Download URL: django-yearmonth-widget-0.2.0.tar.gz
  • Upload date:
  • Size: 8.0 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-yearmonth-widget-0.2.0.tar.gz
Algorithm Hash digest
SHA256 47b4061967e39e6954e7376a105432b6c5291fc9838e768910f9bf4aaec3496e
MD5 f0f4d8712a3a77709053ada1978ebaf8
BLAKE2b-256 6bffe955b73425e578d562351aca9451aeb45744b43bd29b053088601b05a954

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