Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Dynamic Django forms and filters using JSON schema

Project description

Capybara forms

Dynamic Django forms and filters using JSON schema.

If you have 2 models: Category and Entry. And some entry features depends on it's category. Using this library, you will obtain easy-to-configure schema with form, filter and entry fields generators. Also, it can work with model fields.

Quick start

1. Define your models

from django.db import models
from capybara_forms.models import CapybaraFormsCategory, CapybaraFormsModel


class Category(CapybaraFormsCategory):
    title = models.CharField(max_length=100)

class Advert(CapybaraFormsModel(Category)):
    # Global model fields
    title = models.CharField(max_length=100)
    price = models.PositiveSmallIntegerField()
    # Rest fields will be stored in JSON 'data' field

2. Write schema for category 'Cats'

[
  {
    "name": "breed",
    "type": "string",
    "required": true,
    "display_name": "Breed",
    "placeholder": "Enter breed of your cat"
  },
  {
    "name": "year",
    "type": "number",
    "required": true,
    "max_value": 2020,
    "min_value": 1980,
    "placeholder": "Enter birth year of your cat",
    "display_name": "Birth year"
  },
  {
    "name": "weight",
    "type": "number",
    "placeholder": "Enter the weight of your cat",
    "display_name": "Weight"
  },
  {
    "name": "height",
    "type": "number",
    "placeholder": "Height (only placeholder, without display name)"
  },
  {
    "type": "color",
    "display_name": "Cat's color"
  },
  {
    "name": "name",
    "type": "string",
    "placeholder": "Enter cat nickname",
    "display_name": "Name"
  },
  {
    "name": "vaccinated",
    "type": "checkbox",
    "display_name": "is vaccinated"
  }
]

3. Create form. It's a basic rendering object

from django import forms
from capybara_forms.forms import CapybaraFormsModelForm
from .models import Advert


class AdvertForm(CapybaraFormsModelForm):
    # This fields from model will be rendered using form.render_form
    fields_in_model = ['title', 'price']

    # For custom placeholder
    title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter title'}))

    class Meta:
        model = Advert
        fields = ('title', 'price')

4. Use this form in your view

from django.shortcuts import render, get_object_or_404, redirect

from .models import Category
from .forms import AdvertForm


def add(request, category_id):
    category = get_object_or_404(Category, pk=category_id)
    if request.method == 'POST':
        form = AdvertForm(category, request.POST.dict())
        if form.is_valid():
            form.save()
            return redirect('/{0}/'.format(category_id))
    else:
        form = AdvertForm(category)

    return render(request, 'add.html', {
        'form': form,
    })

5. Render form in your template. Note, that library uses parsley for validation. All dependencies listed below

<form method="POST" data-parsley-validate>
    {% csrf_token %}
    {{ form.render_form | safe }}

    <button type="submit">Submit</button>
</form>

Result form:
Form image

Result filter, with similar schema, plus some in-form configuration:
Filter image

Dependencies

  1. Django
  2. jQuery (version 3.3.1 included)
  3. JSON editor (version 5.9.6 included)
  4. parsley (version 2.8.1 included)

Documentation and full example

See readthedocs for API, advanced usage cases, parameters definition and more information

See example project for all features, used in worked application

Slogan

With goal to simplify, with love to capybaras

Project details


Release history Release notifications

This version

0.0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for capybara-forms, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size capybara_forms-0.0.2-py3-none-any.whl (244.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size capybara_forms-0.0.2.tar.gz (234.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page