Implemented a comprehensive update to handle Nepali (Bikram Sambat) localization more robustly across models, forms, admin, and API serializers. This includes native support for Devanagari script output and a complete documentation overhaul. Models & Fields: - Added [NepaliFieldMixin](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:15:0-36:39) to support [ne](cci:1://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:146:4-163:67) (Nepali) and [en](cci:1://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/widgets.py:4:0-10:16) (English) toggles for all custom fields. - Updated [NepaliDateField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:48:0-134:44), [NepaliDateTimeField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:144:0-217:44), and [NepaliTimeField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:137:0-141:34) with the new mixin and improved deconstruction. - Enhanced address fields ([ProvinceField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:220:0-262:23), [DistrictField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:265:0-286:66), [MunicipalityField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/models.py:289:0-310:66)) to support bilingual choices. Admin Enhancements: - Refactored date formatting into a common utility supporting Devanagari via `strftime_ne`. - Updated [NepaliModelAdmin](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:186:0-294:9) and [NepaliAdminMixin](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:118:0-183:75) to auto-detect [ne](cci:1://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:146:4-163:67) settings from model fields for [list_display](cci:1://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:230:4-250:21). - Improved widget injection in the admin to respect field-level localization settings. - Added a JS bridge for admin jQuery compatibility with the Nepali datepicker. Forms & Widgets: - Introduced [NepaliWidgetMixin](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/widgets.py:13:0-38:12) to handle localization data attributes in the frontend. - Updated [NepaliDatePickerWidget](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/widgets.py:63:0-99:42) with improved asset loading and Devanagari support. - Fixed [NepaliDateFormField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/forms.py:10:0-41:13) to correctly handle `empty_value` passed by the Django admin. DRF (Serializers): - Added [BaseNepaliBSField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/serializers.py:21:0-106:48) to DRF serializers with support for `ne=True` for Devanagari output. - Implemented specialized [NepaliDateSerializerField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/serializers.py:114:0-122:28) and [NepaliDateTimeSerializerField](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/serializers.py:130:0-138:32). Documentation: - Completely revamped [README.md](cci:7://file:///home/sankalp/Projects/prod-django-nepkit/README.md:0:0-0:0) with professional design, badges, and detailed usage guides. - Added explicit instructions for address chaining URLs and [NepaliAdminMixin](cci:2://file:///home/sankalp/Projects/prod-django-nepkit/django_nepkit/admin.py:118:0-183:75) usage. Misc: - Optimized address chaining JS logic. - Updated example demo app models and migrations to reflect new field capabilities.
204 lines
5.3 KiB
Markdown
204 lines
5.3 KiB
Markdown
# 🇳🇵 django-nepkit
|
|
|
|
[](https://badge.fury.io/py/django-nepkit)
|
|
[](https://pypi.org/project/django-nepkit/)
|
|
[](https://opensource.org/licenses/MIT)
|
|
|
|
**The essential toolkit for Django developers building for Nepal.**
|
|
|
|
`django-nepkit` handles the specific localization needs of Nepali web applications with elegance and ease. From correct Bikram Sambat (BS) date handling to smart address management and phone validation, we've got you covered.
|
|
|
|
---
|
|
|
|
## ✨ Key Features
|
|
|
|
- **📅 Bikram Sambat (BS) Support**: Full support for Nepali dates and datetimes in models and forms.
|
|
- **📱 Smart Validation**: Built-in validators for Nepali phone numbers.
|
|
- **🗺️ Administrative Locations**: Chained selects for Provinces, Districts, and Municipalities.
|
|
- **🇳🇵 Bilingual Support**: Seamlessly switch between English and Nepali (Devanagari) output for locations and dates.
|
|
- **🔌 Zero-Config Admin**: `NepaliModelAdmin` automatically integrates the Nepali datepicker and formats list displays.
|
|
- **🚀 API Ready**: Optional DRF serializers with Devanagari support.
|
|
|
|
---
|
|
|
|
## 🛠 Installation
|
|
|
|
Install via pip:
|
|
|
|
```bash
|
|
pip install django-nepkit
|
|
```
|
|
|
|
### Add to `INSTALLED_APPS`
|
|
|
|
In your `settings.py`:
|
|
|
|
```python
|
|
INSTALLED_APPS = [
|
|
# ...
|
|
"django.contrib.staticfiles",
|
|
"django_nepkit",
|
|
]
|
|
```
|
|
|
|
---
|
|
|
|
## 🚀 Quick Start
|
|
|
|
### 1. Model Fields
|
|
|
|
Define your models with Nepali-specific requirements effortlessly.
|
|
|
|
```python
|
|
from django.db import models
|
|
from django_nepkit import (
|
|
NepaliDateField,
|
|
NepaliDateTimeField,
|
|
NepaliPhoneNumberField
|
|
)
|
|
|
|
class Citizen(models.Model):
|
|
name = models.CharField(max_length=100)
|
|
|
|
# Stores dates as YYYY-MM-DD strings (BS)
|
|
dob = NepaliDateField()
|
|
|
|
# Use ne=True for Devanagari digits/names in forms/admin
|
|
appointment = NepaliDateTimeField(ne=True)
|
|
|
|
# Validates correct Nepali phone patterns
|
|
phone = NepaliPhoneNumberField()
|
|
```
|
|
|
|
### 2. Admin Integration
|
|
|
|
`NepaliModelAdmin` provides a zero-config experience. It:
|
|
- Automatically attaches the **Nepali Datepicker**.
|
|
- Formats `NepaliDateField` and `NepaliDateTimeField` in `list_display`.
|
|
- Registers `NepaliDateFilter` for easy year-based filtering.
|
|
|
|
```python
|
|
from django.contrib import admin
|
|
from django_nepkit import NepaliModelAdmin
|
|
from .models import Citizen
|
|
|
|
@admin.register(Citizen)
|
|
class CitizenAdmin(NepaliModelAdmin):
|
|
list_display = ("name", "dob", "phone")
|
|
```
|
|
|
|
> [!TIP]
|
|
> If you already have a custom base Admin class, use **`NepaliAdminMixin`** to get all formatting and filter benefits.
|
|
|
|
---
|
|
|
|
## 📝 Forms & Widgets
|
|
|
|
Need to build a custom form? `django-nepkit` provides specialized fields and widgets.
|
|
|
|
```python
|
|
from django import forms
|
|
from django_nepkit.forms import NepaliDateFormField, NepaliPhoneNumberFormField
|
|
from django_nepkit.widgets import NepaliDatePickerWidget
|
|
|
|
class RegistrationForm(forms.Form):
|
|
# Field with built-in validation
|
|
birth_date = NepaliDateFormField()
|
|
|
|
# Or apply the widget to a standard field
|
|
event_date = forms.CharField(
|
|
widget=NepaliDatePickerWidget(ne=True) # Devanagari support
|
|
)
|
|
|
|
phone = NepaliPhoneNumberFormField()
|
|
```
|
|
|
|
---
|
|
|
|
## 🗺️ Address Management (Chained Selects)
|
|
|
|
Model Nepal's administrative structure with automatic filtering: Province → District → Municipality.
|
|
|
|
### Step 1: Define Fields
|
|
|
|
```python
|
|
from django_nepkit import ProvinceField, DistrictField, MunicipalityField
|
|
|
|
class Address(models.Model):
|
|
# For English names
|
|
province = ProvinceField()
|
|
district = DistrictField()
|
|
municipality = MunicipalityField()
|
|
|
|
# For Devanagari (बगती, काठमाडौँ, etc.)
|
|
# province = ProvinceField(ne=True)
|
|
```
|
|
|
|
### Step 2: Include URLs (Crucial)
|
|
|
|
For the chained selects to fetch data dynamically, you **must** include the package URLs in your project's `urls.py`:
|
|
|
|
```python
|
|
# urls.py
|
|
from django.urls import include, path
|
|
|
|
urlpatterns = [
|
|
# ...
|
|
path("nepkit/", include("django_nepkit.urls")),
|
|
]
|
|
```
|
|
|
|
---
|
|
|
|
## 🔌 API Support (Django REST Framework)
|
|
|
|
Install the optional dependency for serializer fields:
|
|
```bash
|
|
pip install "django-nepkit[drf]"
|
|
```
|
|
|
|
Use them in your serializers with optional Devanagari (`ne=True`) output:
|
|
|
|
```python
|
|
from rest_framework import serializers
|
|
from django_nepkit.serializers import NepaliDateSerializerField
|
|
|
|
class CitizenSerializer(serializers.ModelSerializer):
|
|
# Output: "२०८१-१०-१७" if ne=True
|
|
dob = NepaliDateSerializerField(format="%Y-%m-%d", ne=True)
|
|
|
|
class Meta:
|
|
model = Citizen
|
|
fields = "__all__"
|
|
```
|
|
|
|
---
|
|
|
|
## 🛠️ Utility Functions
|
|
|
|
Need to format a date in your logic or views?
|
|
|
|
```python
|
|
from django_nepkit import format_nepali_date
|
|
|
|
# Result: "Magh 17, 2081" (or "माघ १७, २०८१" if ne=True)
|
|
formatted = format_nepali_date(my_date, format_string="%B %d, %Y", ne=False)
|
|
```
|
|
|
|
---
|
|
|
|
## 🤝 Contributing
|
|
|
|
We love contributions!
|
|
|
|
1. Clone the repo: `git clone https://github.com/S4NKALP/django-nepkit`
|
|
2. Install dependencies: `uv sync`
|
|
3. Run the tests: `uv run pytest`
|
|
4. Run the example project: `cd example && python manage.py runserver`
|
|
|
|
---
|
|
|
|
## 📄 License
|
|
|
|
MIT License. Made with ❤️ for the Nepali Django community.
|