Django Extra Views

0.16.0 · active · verified Thu Apr 16

Django Extra Views provides a collection of useful, advanced class-based views for Django, extending the built-in generic views. It's particularly strong for handling complex formsets and inlines. The current version is 0.16.0, and releases are typically feature-driven, providing new views or compatibility updates for Django.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `CreateWithInlinesView` to create a parent object along with its related child objects in a single form. It defines a `Parent` model and a `Child` model, then sets up an `InlineFormSetFactory` for `Child` and integrates it into `CreateWithInlinesView`. Remember to define corresponding URL patterns and templates in your Django project.

from django.db import models
from django.views.generic import ListView
from extra_views import CreateWithInlinesView, InlineFormSetFactory

# models.py example
class Parent(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
    item = models.CharField(max_length=100)

    def __str__(self):
        return f'{self.item} ({self.parent.name})'

# views.py example
class ChildInline(InlineFormSetFactory):
    model = Child
    fields = ['item']
    factory_kwargs = {'extra': 1}

class CreateParentWithChildrenView(CreateWithInlinesView):
    model = Parent
    fields = ['name']
    inlines = [ChildInline]
    template_name = 'parent_create_with_children.html'
    success_url = '/parents/' # Or reverse_lazy('parent-list')

# urls.py example
# from django.urls import path
# from .views import CreateParentWithChildrenView
#
# urlpatterns = [
#     path('parents/create/', CreateParentWithChildrenView.as_view(), name='parent-create-with-children'),
# ]

# parent_create_with_children.html (simplified fragment)
# <form method="post">
#     {% csrf_token %}
#     {{ form.as_p }}
#     <h3>Children</h3>
#     {{ inlines.childinline.management_form }}
#     {% for formset in inlines.childinline %}
#         {{ formset.as_p }}
#     {% endfor %}
#     <button type="submit">Save</button>
# </form>

# To make this runnable, we need a minimal Django setup.
# For testing purposes, you could try:
# from django.conf import settings
# if not settings.configured:
#     settings.configure(DEBUG=True, INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.admin', 'django.contrib.messages'], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}})
# import django
# django.setup()
# # Then define Parent and Child models and run migrations implicitly.
# from django.core.management import call_command
# call_command('makemigrations', 'your_app_name', interactive=False)
# call_command('migrate', interactive=False)
# # This example is primarily for demonstrating the view setup.

view raw JSON →