Django crispy forms with dynamic one to many relationships.
Project description
Add dynamic one-to-many relationships to your crispy forms with easy model declaration. It handles saving, updating and deleting automatically.
Installation
This app can be installed and used in your django project by:
$ pip install django-crispy-dynamic-relationships
Edit your settings.py file to include ‘django-crispy-dynamic-relationships’ in the INSTALLED_APPS listing.
INSTALLED_APPS = [
...
'django-crispy-dynamic-relationships',
]
Subclass ModelFormCrispy to build forms similar to ModelForm. Add an extra field for the one-to-many relationship. Multiple fields supported.
from django-crispy-dynamic-relationships.factory import ModelFormCrispy
class Child1Form(ModelFormCrispy):
class Meta:
model = models.Child1
fields ='__all__'
class ParentForm(ModelFormCrispy):
class Meta:
model = models.Parent
fields ='__all__'
children = [Child1Form]
Instantiate Forms with the parent class (ParentForm in this example) and a model instance if updating. Then pass the instance to the view context.
from django_crispy_forms_dynamic.factory import Forms
context['parent_instance'] = Forms(parent_class=ParentForm, parent_instance=parent_instance)
Load ‘dynamic_modelform_tags’ in your template and apply the filter to the parent_instance.
{% load dynamic_modelform_tags %}
<form method="post">
{% csrf_token %}
{{ forms_factory|form_dynamic }}
<button type="submit" id="id_submit">Submit</button>
</form>
Finally, save your form by calling is_valid() and save() methods in your POST method of your view.
def post(self,request,pk=None):
print(request.POST)
try:
parent_instance = Parent.objects.get(pk=pk)
except Parent.DoesNotExist:
parent_instance = None
factory= Forms(parent_class=ParentForm, parent_instance=parent_instance, form_data=request.POST)
if factory.is_valid():
factory.save()
return redirect('success')
Docs & Source
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-crispy-dynamic-relationships-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad42464a68be72ecb0dd5e4c1d8651c63c9267327b96f0efc0fd5495b3978e4f |
|
MD5 | 00b8e5ee5b74e08956e6ff35e9c8425e |
|
BLAKE2b-256 | 1a4397ab6d4777549b2389bbf6edb4b5950486c44c7b362d944eb4530d848ebc |
Hashes for django_crispy_dynamic_relationships-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63d86ec1c3fd1a13f30e3df90dfe901aecaa9a7dde700a19fb38e532ad1bb925 |
|
MD5 | 72944e431ebe1caff0b21966e63c4ead |
|
BLAKE2b-256 | 2e3d6ea72a2c2f48aafb40be41c0884abf5ecc052365697327e6a2d73d5871d2 |