An opinionated implementation of JSONField for arbitrary HTML element attributes.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.tx Addon code alignments (#31) Jan 22, 2019
djangocms_attributes_field Addon code alignments (#31) Jan 22, 2019
tests Addon code alignments (#31) Jan 22, 2019
.coveragerc added new test setup Dec 13, 2018
.editorconfig Addon code alignments (#31) Jan 22, 2019
.travis.yml Addon code alignments (#31) Jan 22, 2019
CHANGELOG.rst Addon code alignments (#31) Jan 22, 2019
LICENSE.txt Add project files (still no code) Jun 15, 2016
README.rst Addon code alignments (#31) Jan 22, 2019
preview.gif Addon code alignments (#31) Jan 22, 2019 Addon code alignments (#31) Jan 22, 2019
tox.ini Addon code alignments (#31) Jan 22, 2019


django CMS Attributes Field

pypi build coverage

An opinionated implementation of JSONField for arbitrary HTML element attributes.

This project aims to provide a sensible means of storing and managing arbitrary HTML element attributes for later emitting them into templates.

There are a wide variety of types of attributes and using the "normal" Django method of adding ModelFields for each on a business model is cumbersome at best and moreover may require related tables to allow cases where any number of the same type of attribute should be supported (i.e., data-attributes). This can contribute to performance problems.

To avoid these pitfalls, this package allows all of these attributes to be stored together in a single text field in the database as a JSON blob, but provides a nice widget to provide an intuitive, key/value pair interface and provide sensible validation of the keys used.



This is a an open-source project. We'll be delighted to receive your feedback in the form of issues and pull requests. Before submitting your pull request, please review our contribution guidelines.

We're grateful to all contributors who have helped create and maintain this package. Contributors are listed at the contributors section.

One of the easiest contributions you can make is helping to translate this addon on Transifex.


See REQUIREMENTS in the file for additional dependencies:

python django djangocms


For a manual install:

  • run pip install djangocms-attributes-field
  • add djangocms_attributes_field to your INSTALLED_APPS
  • run python migrate djangocms_attributes_field



To use this field in your Models.model:

from django.db import models
from djangocms_attributes_field.fields import AttributesField
    attributes = AttributesField()

That's it!

There is an optional parameter that can be used when declaring the field:

``excluded_keys`` : This is a list of strings that will not be accepted as
                    valid keys
property: [field_name]_str

AttributeField will also provide a handy property [field_name]_str that will emit the stored key/value pairs as a string suitable for inclusion in your template for the target HTML element in question. You can use it like this:

    html_attributes = AttributesField()

# templates/my_cool_project/template.html
<a href="..." {{ object.html_attributes_str }}>click me</a>

(Assuming that object is a context variable containing a MyCoolModel instance.)

In addition to nicely encapsulating the boring task of converting key/value pairs into a string with proper escaping and marking-safe, this property also ensures that existing key/value pairs with keys that have since been added to the field's excluded_keys are also not included in the output string.


The AttributesWidget is already used by default by the AttributesField, but there may be cases where you'd like to override its usage.

The widget supports two additional parameters:

``key_attrs`` : A dict of HTML attributes to apply to the key input field
``val_attrs`` : A dict of HTML attributes to apply to the value input field

These can be useful, for example, if it is necessary to alter the appearance of the widget's rendered appearance. Again, for example, let's say we needed to make the key and value inputs have specific widths. We could do this like so in our ModelForm:


from django import forms
from djangocms_attributes_field.widgets import AttributesWidget

    class Meta:
        fields = ['attributes', ...]

    def __init__(self, *args, **kwargs):
        super(MyCoolForm, self).__init__(*args, **kwargs)
        self.fields['attributes'].widget = AttributesWidget(key_attrs={'style': 'width:250px'},
                                                            val_attrs={'style': 'width:500px'})

Running Tests

You can run tests by executing:

virtualenv env
source env/bin/activate
pip install -r tests/requirements.txt
python test