-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
81 lines (69 loc) · 2.65 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from django.forms import BaseModelFormSet
from django.forms import modelformset_factory
from django.shortcuts import render
from .models import User
class BaseUserFormSet(BaseModelFormSet):
# modelformset_factory() unfortunately has no way to set queryset & prevent loading from the db
def get_queryset(self):
return User.objects.none()
def bulk_create_users(request):
UserFormSet = modelformset_factory(
model=User,
formset=BaseUserFormSet,
fields="__all__",
min_num=1,
extra=0,
)
if request.method == "POST" and "add-row" in request.POST:
# extra is zero-based, so extra = current total since total = min_num + extra
UserFormSet.extra = int(request.POST["form-TOTAL_FORMS"])
# preserve user input without binding the form by loading it as initial
initial = [
{
field_name: request.POST.get(f"form-{i}-{field_name}")
for field_name in UserFormSet.form.base_fields
}
for i in range(int(request.POST["form-TOTAL_FORMS"]))
]
formset = UserFormSet(initial=initial)
elif request.method == "POST":
formset = UserFormSet(data=request.POST)
num_populated_forms = sum(
filter(
None,
(
all(
request.POST.get(f"form-{i}-{field_name}", "") != ""
for field_name in UserFormSet.form.base_fields
)
for i in range(int(request.POST["form-TOTAL_FORMS"]))
),
)
)
if num_populated_forms >= 1:
for form in formset:
form.empty_permitted = True
if formset.is_valid():
formset.save()
users = [form.instance for form in formset.forms]
return render(request, "bulk_create_form/success.html", {"users": users})
else:
formset = UserFormSet()
return render(request, "bulk_create_form/bulk_create.html", {"formset": formset})
def bulk_create_users_js(request):
UserFormSet = modelformset_factory(
model=User,
formset=BaseUserFormSet,
fields="__all__",
min_num=1,
extra=0,
)
if request.method == "POST":
formset = UserFormSet(data=request.POST)
if formset.is_valid():
formset.save()
users = [form.instance for form in formset.forms]
return render(request, "bulk_create_form/success.html", {"users": users})
else:
formset = UserFormSet()
return render(request, "bulk_create_form/bulk_create_js.html", {"formset": formset})