I have two update views accessed with different urls. In the first update view CreateType
I set a variable in my initial called old_type
. After I submit and go through other views I end up in my second UpdateView CreateSchedule
. if i print self.initial in my second view I find the var old_type
I have set in my first UpdateView for some reason.
First view
class CreateType(views.UpdateView):
"""
Create type form
"""
model = Group
form_class = TypeForm
template_name = 'create.html'
extra_context = {'has_fifth_steps': False, 'active_step': 1, 'progress': 0}
def get_object(self, queryset=None):
object = super(CreateType, self).get_object(queryset)
self.initial['old_type'] = object.type
return object
def form_valid(self, form):
object = form.save()
if object.type == Group.TYPE_SINGLE and self.initial['old_type'] == Group.TYPE_MULTIPLE:
first_obj = Obj.objects.filter(group=object).values('id').first()
if first_obj:
Obj.objects.filter(group=object).exclude(id=first_obj['id']).delete()
return redirect('create_obj.debtor', pk=object.pk)
My second view:
class CreateSchedule(views.UpdateView):
"""
Schedule form
"""
model = Group
fields = []
template_name = 'createschedule.html'
def get_object(self, queryset=None):
object = super(CreateSchedule, self).get_object(queryset)
# if object.type == Group.TYPE_SINGLE:
self.obj_object = Obj.objects.filter(group=object).first()
return object
def get_form(self, form_class=None):
paymentschedule_formset = modelformset_factory(
model=PaymentSchedule,
form=PaymentScheduleForm,
fields=('due_date', 'value'),
validate_min=True,
min_num=self.obj_object.number_of_payments,
validate_max=True,
max_num=self.obj_object.number_of_payments,
)
print(self.initial)
return paymentschedule_formset(
queryset=PaymentSchedule.objects.filter(obj=self.obj_object),
)
def get_context_data(self, **kwargs):
context = super(CreateSchedule, self).get_context_data(**kwargs)
if self.object.type == Group.TYPE_MULTIPLE:
context['has_fifth_steps'] = True
context['active_step'] = 5
else:
context['has_fifth_steps'] = False
context['active_step'] = 4
context['progress'] = 100
return context
Not sure if I am doing something wrong or this is a normal behavior which doesn't make much sense to me.
Appreciate any help!
Edit:
Added urls.py
urlpatterns = [
path('create/<int:pk>/', sanad.CreateType.as_view(), name='create_obj.type'),
path('create/<int:pk>/payments-schedule', CreatesSchedule.as_view(), name='create_obj.schedule'),
]