Skip to main content

Django-Hacker: customize default django forms

Project description

Basic usage

Install with pip install djhacker and then:

import djhacker

djhacker.formfield(
    YourModel.your_field,
    form_class=YourFormField,
    custom_form_field_kwarg='something',
)

This will make any Django ModelForm render a YourFormField(custom_form_field_kwarg='something') by default, plus whatever other kwargs it wants to add, you won’t have to use any specific model form, this will work natively in the admin for instance.

Custom formfield callback

You can register custom form field for model field types:

@djhacker.register(models.ForeignKey)
def custom_fk_formfield(model_field, **kwargs):
    return dict(
        form_class=YourFormField,
        custom_form_field_kwarg=something,
        **kwargs,
    )

# you don't need to pass extra arguments anymore for ForeignKey fields:
djhacker.formfield(YourModel.some_fk, queryset=Some.objects.all())

Widget Script attributes

Another thing Django is not doing anytime soon is letting you customize script tags. Which means there’s no easy way to combine Widget.Media.js and any of the nice new script tag attributes, including, but not limited to:

  • async, defer: good to control when your script is loaded

  • type="module": to load a script as an EcmaScript Module (ESM) and use imports

Let’s have this anyway, first patch Django’s Media render_js:

import djhacker
djhacker.media_script_attributes()

Then, let’s customize a script tag:

class YourWidget(forms.Widget):
    class Media:
        js = [
            'your/script.js[type=module][defer=true]',
        ]

It will render as such:

<script src="/static/your/script.js" type="module" defer="true"></script>

Upgrade

To v0.2.x

Registered callbacks now return a simple dict with the form field class in form_class.

@djhacker.register(models.ForeignKey)
def custom_fk_formfield(model_field, **kwargs):
    return YourFormField, {
        'custom_form_field_kwarg': 'something',
        **kwargs,
    )

Becomes:

@djhacker.register(models.ForeignKey)
def custom_fk_formfield(model_field, **kwargs):
    return dict(
        form_class=YourFormField,
        custom_form_field_kwarg=something,
        **kwargs,
    )

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

djhacker-0.2.3.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file djhacker-0.2.3.tar.gz.

File metadata

  • Download URL: djhacker-0.2.3.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for djhacker-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4644245c23991dfc9e065b22185e1addf171ebe671beb6b2d41750e0a62e0c6f
MD5 acd2c26ed91e0052a6f8f5830e08ced3
BLAKE2b-256 35fda9a02c3d9e5fb8eeaba773fd5e557914b20d2147bf88a2d9e1ecfff9d320

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