Giter Site home page Giter Site logo

django-nested-forms's Introduction

Django nested forms

A util to make Django formsets easy to use.

Overview

Django provides powerful support for creating a complex app with simple code, among which is support for creating a page with a form for users to fill in. Although the framework provides a tool called a formset, which is a collection of forms, to help build a page that has multiple forms, working with formsets is a little bit more complex than working with normal forms and requires some boilerplate code. This package aims to help people use formsets with a minimum amount code and make formsets even easier to use.

Usage

Let's assume you have a model named Book and a model named Author. To allow the user to edit several books at once, you use a decorator from this package in your forms.py (or in your views.py if you do not have a separate module for forms):

from django import forms
from nested_forms import ModelFormSetHandler

from .models import Author, Book

BookFormSet = forms.modelformset_factory(
    model=Book,
    fields=['title', 'description']
)

@ModelFormSetHandler(
    BookFormSet, # specify the formset to include
    'book' # specify how we refer to the formset in our template later
)
class AuthorForm(forms.ModelForm):

    class Meta:
        model = Author
        fields = ['first_name', 'last_name']

You do not have to do nothing special in your views.py:

from django.views.generic.edit import UpdateView

from .models import Author
from .forms import AuthorForm

class UpdateAuthorView(UpdateView):

    model = Author
    form_class = AuthorForm
    template_name = 'author-form.html'

In your template access the book formset like this:

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.formsets.book }}
    {{ form }}
    <button type="submit">Submit</button>
</form>

That's it. The decorator nested_forms.ModelFormSetHandler handles all the complexity for you. All you have to do is to apply a decorator to a form class, create your view just as usual, and display your formset as you like in your template.

Installing

Using pip:

$ pip install git+https://github.com/ykiu/django-nested-forms

Tested with...

This package has been tested with

  • Python 3.6
  • Django 1.11

Misc

This package is still under development. The APIs may change in the future and the code can contain a host of bugs (I'd be happy if you could send me a bug report).

Contribution

To controbute, fork the repo, do your work and make a pull request. Remember to include tests for any new features or bug fixes. Your contribution would be greatly appreciated:-)

django-nested-forms's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.