Sorry for such a rookie question, but I'm stuck and I'm reaching out. I've used the FormWizard to capture subscription data from users. Works great. Now I want them to be able to update their subscription using the same FormWizard.
I'm able to show their previous inputs, however, when it comes to actually knowing which record to update, that's where I'm having trouble. I am able to get the id from the URL in the view function for that path, but I'm having trouble getting the id to other views.
My code is below. I'm stuck on section 9.3. I'm not sure how to get the record id so it can update the correct record. If there is a better approach, feel free to suggest it and thanks in advance.
urls.py
path('subscription/update/<int:id>/', service_views.wizard_edit, name='wizard-edit'),
views.py
## 9.1 Displaying the data in the form
def wizard_edit(request, id):
## Collecting the data
sub = Subscribers.objects.get(id=id)
## Displaying the data in the appropriate forms
initial = {
'0': {'industry_search':sub.industry},
'1': {'city':sub.city, 'kilometers':sub.kilometers, 'street_1':sub.street_1, 'street_2':sub.street_2},
'2': {'email':sub.email}
}
wiz = ContactWizardUpdate.as_view([ContactForm1, ContactForm2, ContactForm3], initial_dict=initial)
return wiz(request)
## 9.2 FormWizard
class ContactWizardUpdate(SessionWizardView):
template_name = 'service/subscribe.html'
form_list = [ContactForm1, ContactForm2, ContactForm3]
def done(self, form_list, **kwargs):
## Function to update the DB and save data
update_the_form_data(self, form_list)
return render(self.request, 'service/done.html')
## 9.3 Updating the database with the changes
def update_the_form_data(self, form_list):
form_data = [form.cleaned_data for form in form_list]
## Get the correct record for the update
foo = get_object_or_404(Subscribers, id=[THE ID FOR THE RECORD])
## Additional code
foo.save()
I figured out a way to do it. I'll share in case this helps someone else.
Instead of having the path on the urls.py file:
path('subscription/update/<int:id>/', service_views.wizard_edit, name='wizard-edit'),
I changed it to, path('subscription/update/', service_views.wizard_edit, name='wizard-edit'), and added an edit button on the user's subscription summary page with the following path, Edit Subscription
Here are the edits to the views.py file:
## 9.1 Displaying the data in the form
def wizard_edit(request): ## NEW
id_ = request.GET.get('id') ## NEW
## Collecting the data
sub = Subscribers.objects.get(id=id_) ## NEW
## Displaying the data in the appropriate forms
initial = {
'0': {'industry_search':sub.industry},
'1': {'city':sub.city, 'kilometers':sub.kilometers, 'street_1':sub.street_1, 'street_2':sub.street_2},
'2': {'email':sub.email}
}
wiz = ContactWizardUpdate.as_view([ContactForm1, ContactForm2, ContactForm3], initial_dict=initial)
return wiz(request)
## 9.2 FormWizard
class ContactWizardUpdate(SessionWizardView):
template_name = 'service/subscribe.html'
form_list = [ContactForm1, ContactForm2, ContactForm3]
def done(self, form_list, **kwargs):
## Function to update the DB and save data
update_the_form_data(self, form_list)
return render(self.request, 'service/done.html')
## 9.3 Updating the database with the changes
def update_the_form_data(self, form_list):
form_data = [form.cleaned_data for form in form_list]
id_ = self.request.GET.get('id') ## NEW
## Get the correct record for the update
foo = get_object_or_404(Subscribers, id=id_) ## NEW
## Additional code
foo.save()
Related
I have a Model as follows:
class TankJournal(models.Model):
user = models.ForeignKey(User)
tank = models.ForeignKey(TankProfile)
ts = models.IntegerField(max_length=15)
title = models.CharField(max_length=50)
body = models.TextField()
I also have a model form for the above model as follows:
class JournalForm(ModelForm):
tank = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = TankJournal
exclude = ('user','ts')
I want to know how to set the default value for that tank hidden field. Here is my function to show/save the form so far:
def addJournal(request, id=0):
if not request.user.is_authenticated():
return HttpResponseRedirect('/')
# checking if they own the tank
from django.contrib.auth.models import User
user = User.objects.get(pk=request.session['id'])
if request.method == 'POST':
form = JournalForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
# setting the user and ts
from time import time
obj.ts = int(time())
obj.user = user
obj.tank = TankProfile.objects.get(pk=form.cleaned_data['tank_id'])
# saving the test
obj.save()
else:
form = JournalForm()
try:
tank = TankProfile.objects.get(user=user, id=id)
except TankProfile.DoesNotExist:
return HttpResponseRedirect('/error/')
You can use Form.initial, which is explained here.
You have two options either populate the value when calling form constructor:
form = JournalForm(initial={'tank': 123})
or set the value in the form definition:
tank = forms.IntegerField(widget=forms.HiddenInput(), initial=123)
Other solution: Set initial after creating the form:
form.fields['tank'].initial = 123
If you are creating modelform from POST values initial can be assigned this way:
form = SomeModelForm(request.POST, initial={"option": "10"})
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#providing-initial-values
I had this other solution (I'm posting it in case someone else as me is using the following method from the model):
class onlyUserIsActiveField(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(onlyUserIsActiveField, self).__init__(*args, **kwargs)
self.fields['is_active'].initial = False
class Meta:
model = User
fields = ['is_active']
labels = {'is_active': 'Is Active'}
widgets = {
'is_active': forms.CheckboxInput( attrs={
'class': 'form-control bootstrap-switch',
'data-size': 'mini',
'data-on-color': 'success',
'data-on-text': 'Active',
'data-off-color': 'danger',
'data-off-text': 'Inactive',
'name': 'is_active',
})
}
The initial is definded on the __init__ function as self.fields['is_active'].initial = False
As explained in Django docs, initial is not default.
The initial value of a field is intended to be displayed in an HTML . But if the user delete this value, and finally send back a blank value for this field, the initial value is lost. So you do not obtain what is expected by a default behaviour.
The default behaviour is : the value that validation process will take if data argument do not contain any value for the field.
To implement that, a straightforward way is to combine initial and clean_<field>():
class JournalForm(ModelForm):
tank = forms.IntegerField(widget=forms.HiddenInput(), initial=123)
(...)
def clean_tank(self):
if not self['tank'].html_name in self.data:
return self.fields['tank'].initial
return self.cleaned_data['tank']
If you want to add initial value and post other value you have to add the following :
or None after request.POST
form = JournalForm(request.POST or None,initial={'tank': 123})
If you want to add files or images also
form = JournalForm(request.POST or None,request.FILES or None,initial={'tank': 123})
I hope this can help you:
form.instance.updatedby = form.cleaned_data['updatedby'] = request.user.id
I also encountered the need to set default values in the form during development. My solution is
initial={"":""}
form=ArticleModel(request.POST)
if form.has_changed():
data = {i: form.cleaned_data[i] for i in form.changed_data}
data.update({key: val for key, val in init_praram.items() if key not in form.changed_data})
use form.has_changed ,if form.fields is required you can use this method
How I added the initial to the form:
I read #Sergey Golovchenko answer.
So I just added it to the form in if request.method == 'POST':.
But that's not where you place it, if you want to see what value it got before posting the form.
You need to put it in the form where the else is.
Example here from views.py
def myForm(request):
kontext = {}
if request.method == 'POST':
# You might want to use clean_data instead of initial here. I found something on a stack overflow question, and you add clean data to the Forms.py, if you want to change the post data. https://stackoverflow.com/questions/36711229/django-forms-clean-data
form = myModelForm(request.POST, initial={'user': request.user})
if form.is_valid():
form.save()
return redirect('/')
else:
# you need to put initial here, if you want to see the value before you post it
form = myModelForm(initial={'user': request.user})
kontext['form'] = form
return render(request, 'app1/my_form.html', kontext)
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 am trying to write a form that allows the user to select as many users from a specific group as they want. However when I try to use the list of users as an option I get an error saying that 'User' object does not support indexing.
Its a fairly standard form, the main difference is that the group is filtered based on a kwarg passed to the form. The form is passed a project_id (project object primary key) and it then finds the group associated with that project and generates the field.
From forms.py
class ModifyTeamForm(forms.Form):
action = ChoiceField(choices=[('remove', 'Remove users'), ('promote', 'Promote to lead.')])
def __init__(self, *args, **kwargs):
# The project to get the team for
project_id = kwargs.pop('project_id', None)
super(ModifyTeamForm, self).__init__(*args, **kwargs)
project = Project.objects.get(pk=project_id)
# Team for this project
team = User.objects.filter(groups__name=project.project_name)
# Create a form field to select current team members
current_team = MultipleChoiceField(required=True, choices = team, widget=CheckboxSelectMultiple)
# Add the field
self.fields['current_team'] = current_team
My views.py
#login_required
def team(request, project_id):
if request.method == "POST":
# Not yet implemented
return
else:
form = ModifyTeamForm(project_id=project_id)
template = loader.get_template('projects/team.html')
context = RequestContext(request, {
'form': form,
})
return HttpResponse(template.render(context))
It's because MultipleChoiceField.choices is expected to be a 2d Array effectively (https://docs.djangoproject.com/en/1.7/ref/forms/fields/#django.forms.ChoiceField.choices).
So you could do something like this:
team = [(u.pk, u.email) for u in User.objects.filter(groups__name=project.project_name)]
And that will return you a list continaing the combintation of
[('user1.pk', 'user1.email'), ('user2.pk', 'user2.email'),...]
which will be useable as the choices.
I have models for Application and Role. Role is linked to a FK Role_type, which is linked by FK to Applications that can use those Role_types (this is a bit of an over-simplication for the question, but I think it suffices). I need a way to create a form to make a new Application, and also to create records assigning associated roles to people (although they can be left blank.)
I have gotten as far as creating the form for the Application and having the associated Role-Types appear on the page, with dropdowns to be populated with a user. Hitting submit, though, didn't create any of the associated Role records. All of my research seems to keep coming back to Inline Model Forms, but the docs aren't really making sense to me--the inputs in the example don't seem to correlate to what I need.
I know this may seem like a duplicate, but trust me when I say I've looked at every SO question that seems to relate to this!
EDIT: My POST looks like this: QueryDict: {u'roles-MAX_NUM_FORMS': [u'1000'], u'roles-1-role_type': [u'4'], u'roles-0-user': [u'1'], u'app-owner': [u'1'], u'app-name': [u'1234'], u'app-serviceTier': [u''], u'app-jiraProject': [u''], u'roles-TOTAL_FORMS': [u'2'], u'roles-1-user': [u''], u'roles-0-role_type': [u'3'], u'csrfmiddlewaretoken': [u'eGsDwtsSQJfl0'], u'roles-INITIAL_FORMS': [u'2']}>. Printing RolesFormSet gives me the exact same output (see comment below)
models.py
class Item(models.model):
name = models.CharField(max_length=255)
roles = models.ManyToManyField(User, through='Role')
class Application(Item):
other_assorted_attributes = foo
class RoleType(models.Model):
name = models.CharField(max_length=255)
class ItemTypeRoleMapping(models.Model):
''' pairs role-types (e.g., Developer) with an Item class they are relevant to'''
roleType = models.ForeignKey(RoleType)
itemType = models.CharField(max_length=255, choices=itemChoices)
class Role(models.Model):
role_type = models.ForeignKey(RoleType)
user = models.ForeignKey(User)
item = models.ForeignKey(Item)
views.py
def buildRolesFormset(itemClass):
''' given an item, build a form for all associated roles '''
roleTypesForItem = ItemTypeRoleMapping.objects.all().filter(itemType=itemClass.__name__)
applicable_roles = [{'role_type': roleType} for roleType in roleTypesForItem]
# formset = rolesFormSet(initial=initial, prefix='roles')
RoleFormSet = inlineformset_factory(Application, Role, extra=len(roleTypesForItem), can_delete=False)
formset = RoleFormSet()
for subform, data in zip(formset.forms, applicable_roles):
subform.initial = data
return formset
def new(request):
''' Create a new application '''
user = request.user
# check permission
if request.method == 'POST':
appform = AppForm(request.POST, prefix='app')
if appform.is_valid():
app = appform.save(commit=False)
rolesInlineFormSet = inlineformset_factory(Application, Role)
# pdb.set_trace()
rolesFormSet = rolesInlineFormSet(request.POST, instance=app, prefix='roles')
if rolesFormSet.is_valid():
rolesFormSet.save()
else:
print rolesFormSet.errors
app = appform.save()
# check rolesFormSet
return redirect(reverse('index'))
else:
appform = AppForm(prefix='app')
rolesFormSet = buildRolesFormset(Application)
return render(request, 'who/editapp.html',
{'appform': appform,
'rolesFormSet': rolesFormSet
})
Tricky to tell without more information, but it looks like you're not saving your rolesFormset in the view. You need to call rolesFormset.save() alongside your form.save() call. Additionally, I suppose you want to attach the roles to the created app? Something like this in your view should work:
if request.method == 'POST':
form = AppForm(request.POST)
rolesFormset = RoleForm(request.POST)
if form.is_valid() and rolesFormset.is_valid():
app = form.save()
roles = rolesFormset.save()
for role in roles:
app.roles.add(role)
return redirect(reverse('index'))
Update: Presuming the models.py is out-of-date, and Role does in fact have a foreignKey to User, the problem will be that you're setting a prefix here:
rolesFormSet = rolesInlineFormSet(request.POST, instance=app, prefix='roles')
but not in your buildRolesFormset function. In that function, do:
formset = RoleFormSet(prefix='roles')