django-nonefield¶
django-nonefield
is a None field for Django.
A typical use case: allow presentational (non-input, non-field) form elements (text, image, embed video, etc). This could be very useful if your forms are dynamic (as in form-builders/generators). Django REST Framework integration is implemented as well.
Prerequisites¶
- Django 1.8, 1.11, 2.0, 2.1, 2.2, 3.0.
- Python 2.7, 3.5, 3.6, 3.7, 3.8
Installation¶
Install latest stable version from PyPI:
pip install django-nonefield
Or latest stable version from GitHub:
pip install https://github.com/barseghyanartur/django-nonefield/archive/stable.tar.gz
Or latest stable version from BitBucket:
pip install https://bitbucket.org/barseghyanartur/django-fobi/get/stable.tar.gz
Add
nonefield
toINSTALLED_APPS
of the your projects’ Django settings.INSTALLED_APPS = ( # ... # None field 'nonefield', # ... )
Usage¶
forms.py¶
In forms you could use it as follows:
from django import forms
from nonefield.fields import NoneField
class MyForm(forms.Form):
name = forms.CharField(max_length=255)
some_text = NoneField(initial='Lorem ipsum')
See this snippet as an example of how to allow to use paragraphs in the django-forms-builder.
serializers.py¶
You can also use it in Django REST Framework.
from rest_framework import serializers
from nonefield.contrib.drf_integration.fields import NoneField
class ContentTextField(NoneField):
"""Content text field."""
class BarSerializer(serializers.Serializer):
title = serializers.CharField(max_length=256)
text = serializers.CharField()
context_text = ContentTextField(label='', default='Haha')
See how it’s used in django-fobi to allow to use content/presentational elements (text, image, embed video, etc.) in the Django REST Framework schema.
Examples¶
License¶
GPL-2.0-only OR LGPL-2.1-or-later
Author¶
Artur Barseghyan <artur.barseghyan@gmail.com>
Project documentation¶
Contents:
Release history and notes¶
Sequence based identifiers are used for versioning (schema follows below):
major.minor[.revision]
- It’s always safe to upgrade within the same minor version (for example, from 0.3 to 0.3.4).
- Minor version changes might be backwards incompatible. Read the release notes carefully before upgrading (for example, when upgrading from 0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.
0.4¶
2020-01-23
- Added Django REST Framework contrib.
- Tested against Django 2.2 and 3.0.
- Tested against Python 3.8.
nonefield package¶
Subpackages¶
Submodules¶
nonefield.fields module¶
-
class
nonefield.fields.
NoneField
(*, required=True, widget=None, label=None, initial=None, help_text='', error_messages=None, show_hidden_initial=False, validators=(), localize=False, disabled=False, label_suffix=None)[source]¶ Bases:
django.forms.fields.Field
NoneField.
To be used with content elements like text or images, that need to be present, for instance, in between form input elements.
-
bound_data
(data, initial)[source]¶ Bound data.
Return the value that should be shown for this field on render of a bound form, given the submitted POST data for the field and the initial data, if any.
For most fields, this will simply be data; FileFields need to handle it a bit differently.
-
clean
(value)[source]¶ Validate the given value and return its “cleaned” value as an appropriate Python object. Raise ValidationError for any errors.
-
widget
¶ alias of
nonefield.widgets.NoneWidget
-