Metadata-Version: 2.1
Name: django-formsets-inside-form
Version: 0.1.3
Summary: A library to handle formsets within forms in Django
Home-page: https://github.com/DeividHugo/django-formsets-inside-form
Author: Deivid Hugo
Author-email: deividhugoof@gmail.com
License: MIT
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=2.0

# Django Formsets Inside Form Mixin

## Overview

The Formsets Inside Form Mixin is a Django mixin designed to manage inline formsets and atomic transactions in an integrated manner. This mixin provides functionality to handle the saving of the main form's instance and its associated inline formsets within a single atomic transaction, ensuring data integrity and consistency.

## How it Works

### Initialization

1. **Initialization**: Upon initialization of the form, the mixin sets up the formsets by calling the `get_formsets()` method.

2. **Formsets Setup**: The mixin's `get_formsets()` method initializes and returns the formsets associated with the form. These formsets are stored as attributes of the form instance, making them accessible throughout the form's lifecycle.

### Validation

1. **Validation**: The `is_valid()` method is overridden to include validation for both the main form and its associated formsets.

2. **Formset Validation**: Each formset is iterated through, and if any formset is not valid, its errors are collected and stored in the main form's errors dictionary under the key 'formset_errors'.

### Saving

1. **Atomic Transaction**: The `save()` method is decorated with Django's `@transaction.atomic` decorator, ensuring that the saving process occurs within a single atomic transaction, thus maintaining data integrity.

2. **Instance Saving**: The main form's instance is saved within the atomic transaction.

3. **Formsets Saving**: Each formset associated with the form is saved within the atomic transaction.

## Declaration of Formsets

To utilize this mixin, the form must declare its formsets within the `get_formsets()` method. The mixin provides a convenient way to manage formsets within the form, making them accessible and manageable throughout the form's lifecycle.

## Example Usage

```python
from django import forms
from django.db import transaction

class YourForm(FormsetsInsideFormMixin, forms.Form):
    # Your form fields go here

    def get_formsets(self, formsets={}, instance=None, *args, **kwargs):
        # Define your formsets here
        # Example:
        # formsets = {
        #     'formset_name': YourFormSet(data=self.data, instance=instance, *args, **kwargs),
        # }
        return formsets
```

## Conclusion

The Formsets Inside Form Mixin provides a structured and integrated approach to manage inline formsets within Django forms. By encapsulating formsets management and atomic transactions handling, it promotes code organization, maintainability, and ensures data consistency during the form submission and saving process.
