I am trying to create a SessionWizardView for a trip creation process. The trip might have one leg (one way) or two legs (round trip). Each leg has similar schema so I would like to use the same Form for both step 0 and step1, with a condition saying only to use step1 when the flight is round trip.
The problem I am having is that my "submit" button keeps loading step 0 over and over again instead of moving on to step 1 as it should for a round trip flight. (I am prepopulating each of the forms based on previously requested trip info for each leg in the get_form_initial() override). My form populates correctly for the first leg, it just populates the first leg data on every submit ad infinitum.
I could make two identical forms, but that seems like poor practice. Slightly better, I could have the Return trip form just inherit from the Outbound trip form and not make any changes to it - this is what I'll try next barring a better solution.
But, I'm really wondering if there is there a way to use the same form twice?
In my urls.py:
wizard_forms = [TripCreationForm,TripCreationForm]
urlpatterns = patterns('',
url(r'^trip/wizard/(?P<pk>\d+)$',
views.CreateTripSetView.as_view(wizard_forms,
condition_dict= {'1':show_return_trip_form}), name='admin_add_tripset')
)
in views.py:
def show_return_trip_form(wizard):
"""
Tells the CreateTripSetView wizard whether to show the return trip form
Args:
wizard:
Returns: True if this is a round trip, false if one-way
"""
cleaned_data = wizard.get_cleaned_data_for_step('0') or {}
if cleaned_data.get('total_legs') == 2:
return True
return False
class CreateTripSetView(SessionWizardView):
def get_form_initial(self, step):
"""
Populates the initial form data based on the request, route etc.
THIS IS ALWAYS FIRING FOR STEP=0 WHEN I HIT SUBMIT.
Args:
step:
Returns:
"""
initial = self.initial_dict.get(step, {})
triprequest = TripRequest.objects.filter(id=self.kwargs['pk']).first()
if triprequest is None:
return initial
initial.update({
'request_id': flight_request.id,
#other fields set on initial here
})
return initial
in forms.py:
class TripCreationForm
#field defs ex.
request_id = forms.IntegerField()
#etc.
def __init__(self, initial, *args, **kwargs):
object_data = {}
object_data['request_id'] = initial['request_id']
#etc.
super(AnywhereFlightCreationForm, self).__init__(initial=object_data, *args, **kwargs)
Edited:
So far I've been able to make this work using two subclasses of TripCreationForm but not using TripCreationForm for both.
Thanks in advance!
The wizard needs to identify them as separate steps. Maybe this would work?
wizard_forms = [
("form1", TripCreationForm),
("form2", TripCreationForm),
]
Related
I am new to Django Class Based Views and I am working on a project where on the template I want to have Form for creating customer accounts on the left and list of existing customers on the right.
So far I have the list of existing customers displayed but for the form I don't know how to pass its variable context to the same template, or it is not possible to Pass a Form that would be submitted inside a ListView Method. And I also want to generate unique account numbers of 10 Digits in ModelForm which I want the form field to be auto-filled and disabled
Here is my form code:
import secrets
#I want to Generate Account Number of 10 Digits but getting only 2
account = secrets.randbits(7)
#class for Customer Account Form
class CustomerAccountForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().init(*args, **kwargs)
self.fields['accountnumber'].initial = account
class Meta:
model = Customer
fields = ['accountnumber','surname','othernames','address','phone']
Code for my views (ListView)
class CustomerListView(ListView):
model = Customer
form_class = CustomerAccountForm
template_name = 'dashboard/customers.html'
#Function to get context data from queries
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
#Get Day of today from current date and time
now = datetime.datetime.now()
#Get the date today
date_today = datetime.datetime.now().date
#Count Number of Withdrawals Today and passing in context
context_data['count_withdrawals_today'] = Witdrawal.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day).count()
context_data['count_deposits_today'] = Deposit.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day).count()
context_data['count_accounts'] = Customer.objects.count()
context_data['count_users'] = User.objects.count()
#Calculate today Deposit Today
context_data['total_deposit']= Deposit.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day).aggregate(total_deposit=Sum('deposit_amount')).get('total_deposit') or 0
#Calculate today Withdrawal Today
context_data['total_withdrawal']= Witdrawal.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day).aggregate(total_withdrawal=Sum('withdrawal_amount')).get('total_withdrawal') or 0
return context_data
Someone should please help me on how this is properly done and the form would be submitted successfully. Thanks in anticipation for your answer.
secrets.randbits(k) generates a random integer within a bit range.
For k=4 then Unsigned integer From 0 to 15.
For k=8 then Unsigned integer From 0 to 255.
For k=16 then Unsigned integer From 0 to 65,535, and so on.
If you want 10 random digits then you can use for example:
import random
account = "".join(str(random.randint(0, 9)) for _ in range(10))
After going through many tutorials and blogs on Django Class Based Views with ListViews for Forms, I discovered that ListViews was designed to populate Model items while FormViews is designed for creating and processing forms both can't be used on one template. Although many developers have had a way around it with the use of Multiple Mixins but no best practice has be mentioned for or against their findings yet.
In these case, I concluded that for a Django Template to be able to process Model Form and at the same time Populate Database items, it needs not to use Class Based Views but Function Based Views.
Usually, in a wizard, we declare forms or formsets in static way, for example with something like:
form_class=formset_factory(MyForm, min_num=1, extra=5) # let's say this is step '4'
But now, what if I need data from step 3, to know how to define the min_num or extra value for the formset of step 4?
I was thinking of doing such thing in the get_form() method:
def get_form(self, step=None, data=None, files=None):
form = super().get_form(step, data, files)
# ....
elif step == '4':
step3_data = self.storage.get_step_data('3')
# ... here I would parse step 3 data, to be able to define:
computed_step_4_min_num = 5
computed_step_4_extra = 10
# And here I would need to call formset_factory(min_num=computed_step_4_min_num,
extra=computed_step_4_extra),
# but how? This is obviously not the right place for this call.
While it's easy to edit form fields attributes in the get_form() method, I did not find a way to define the right number of forms of a formset, in a dynamic way.
I read documentation but I could have missed it. Thanks for your help.
By reading the documentation and checking the source code, I think this is the optimal solution to use:
def get_form(self, step=None, data=None, files=None):
if step == '4':
step3_data = self.storage.get_step_data('3')
# do calculations
computed_step_4_min_num = 5
computed_step_4_extra = 10
form = formset_factory(MyForm, min_num=computed_step_4_min_num, extra=computed_step_4_extra)
self.form_list[step] = form
return super().get_form(step, data, files)
I am overriding the self.form_list to add the formset factory. But you should add a formset in the view when initiating the Wizard instance:
>> formset = formset_factory(MyForm, min_num=1, extra=1)
>> MyWizardForm.as_view([Form1, Form2, Form3, formset], initial_dict=initial)
I'm using Django Form Wizard in project. I want to allow users to provide initial data for specific field and I also want to make field with initial data provided disabled.
Problem is that when user clicks back button the initial data is erased.
For example this is my form(initial data is set as second step)
As you can see everything is fine here, the form fields are disabled and the value is selected. Now we click next.
We click next and we get to the second step without problems but now if I click back..
Form fields are still disabled but the value is gone!
The code I'm using goes like this (relevant part):
Form
CHOICES=[('ONE','Choice 1'),
('TWO','Choice 2'),
('THREE','Choice 3'),
('FOUR','Choice 4'),
]
class BookingForm1(forms.Form):
""" Select reservation type"""
sko = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
Wizard
def get_form(self, step=None, data=None, files=None):
form = super(PresentView, self).get_form(step, data, files)
step = step or self.steps.current
initial = {
'sko': {
'initial':'TWO'
}
}
for field in initial:
try:
form.fields[field].widget.attrs['readonly'] = True
form.fields[field].widget.attrs['disabled'] = True
form.fields[field].required = False
form.fields[field].initial = initial[field]['initial']
except:
pass
Any ideas would be greatly appreciated!
The way I was able to solve it was by removing extra fields for ChoiceField type objects.
for field in initial:
try:
field_o = form.fields[field]
# Remove extra choices if Choice field
if type(field_o) == forms.fields.ChoiceField:
field_o.choices = [(key, value) for key,value in field_o.choices if key == initial[field]['initial']]
else:
field_o.widget.attrs['readonly'] = True
field_o.initial = initial[field]['initial']
except:
pass
I've got simple project, that do almost nothing now - there is MultipleChoiceField with some Person-values and when user selects some values and clicks "submit" button - I'm sarching for Houses, where selected guys lived.
There is about 1.5k persons and 20k houses in my database. If I select 3+ people from list (in this case, there is about 5-6k values in 'houses' list), processing time is taking very long time (~5-7 seconds for my 't' variable).
As django-debug-toolbar says, DB-queries takes only 0.7 seconds and ~95% of time is 'request' section of 'Timer' panel. So the question is - what am I doing wrong? I think there is wrong way of using render_to_response. Can you recommend me something to optimize my code? I'm new at Django, thank you.
At my 'guess_houses.html' I only display list of houses with their field.
Here is my views.py:
class HouseView(FormView):
template_name = 'guess_houses.html'
form_class = PersonListForm # that's my MultipleChoiceField with persons in it
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
persons_id = form.cleaned_data['persons']
houses= []
t1 = time()
for id_person_dict in House.objects.all().values('id', 'persons', 'address'):
# if persons_id and persons who lived in house got intersection element(s)
if set(persons_id) & set(id_person_dict['persons']):
if id_person_dict not in houses:
houses.append(id_person_dict)
return render_to_response(self.template_name, {'form': form, 'houses': houses, 't': time()-t1})
I have some form fields that I want to include/exclude based on whether or not a certain condition is met. I know how to include and exclude form elements, but I am having difficulty doing it when I want it elements to show based on the outcome of a function.
Here is my form:
class ProfileForm(ModelForm):
# this_team = get Team instance from team.id passed in
# how?
def draft_unlocked(self):
teams = Team.objects.order_by('total_points')
count = 0
for team in teams:
if team.pk == this_team.pk:
break
count += 1
now = datetime.datetime.now().weekday()
if now >= count:
# show driver_one, driver_two, driver_three
else:
# do not show driver_one, driver_two, driver_three
class Meta:
model = Team
What I am trying to accomplish is, based on the standings of total points, a team should not be able to change their driver until their specified day. As in, the last team in the standings can add/drop a driver on Monday, second to last team can add/drop on Tuesday, and so on...
So the first problem -- how do I get the Team instance inside the form itself from the id that was passed in. And, how do I include/exclude based on the result of draft_unlocked().
Or perhaps there is a better way to do all of this?
Thanks a lot everyone.
This is actually fairly straightforward (conditional field settings) - here's a quick example:
from django.forms import Modelform
from django.forms.widgets import HiddenInput
class SomeForm(ModelForm):
def __init__(self, *args, **kwargs):
# call constructor to set up the fields. If you don't do this
# first you can't modify fields.
super(SomeForm, self).__init__(*args, **kwargs)
try:
# make somefunc return something True
# if you can change the driver.
# might make sense in a model?
can_change_driver = self.instance.somefunc()
except AttributeError:
# unbound form, what do you want to do here?
can_change_driver = True # for example?
# if the driver can't be changed, use a input=hidden
# input field.
if not can_change_driver:
self.fields["Drivers"].widget = HiddenInput()
class Meta:
model = SomeModel
So, key points from this:
self.instance represents the bound object, if the form is bound. I believe it is passed in as a named argument, therefore in kwargs, which the parent constructor uses to create self.instance.
You can modify the field properties after you've called the parent constructor.
widgets are how forms are displayed. HiddenInput basically means <input type="hidden" .../>.
There is one limitation; I can tamper with the input to change a value if I modify the submitted POST/GET data. If you don't want this to happen, something to consider is overriding the form's validation (clean()) method. Remember, everything in Django is just objects, which means you can actually modify class objects and add data to them at random (it won't be persisted though). So in your __init__ you could:
self.instance.olddrivers = instance.drivers.all()
Then in your clean method for said form:
def clean(self):
# validate parent. Do this first because this method
# will transform field values into model field values.
# i.e. instance will reflect the form changes.
super(SomeForm, self).clean()
# can we modify drivers?
can_change_driver = self.instance.somefunc()
# either we can change the driver, or if not, we require
# that the two lists are, when sorted, equal (to allow for
# potential non equal ordering of identical elements).
# Wrapped code here for niceness
if (can_change_driver or
(sorted(self.instance.drivers.all()) ==
sorted(self.instance.olddrivers))):
return True
else:
raise ValidationError() # customise this to your liking.
You can do what you need by adding your own init where you can pass in the id when you instantiate the form class:
class ProfileForm(ModelForm):
def __init__(self, team_id, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
this_team = Team.objects.get(pk=team_id)
teams = Team.objects.order_by('total_points')
count = 0
for team in teams:
if team.pk == this_team.pk:
break
count += 1
now = datetime.datetime.now().weekday()
if now >= count:
# show driver_one, driver_two, driver_three
else:
# do not show driver_one, driver_two, driver_three
class Meta:
model = Team
#views.py
def my_view(request, team_id):
profile_form = ProfileForm(team_id, request.POST or None)
#more code here
Hope that helps you out.