Django Form Wizard Dynamic Form Not Saving Data - django

Currently Happening:
Dynamically generated form and form fields are being displayed.
Enter some data into the said fields, but self.get_all_cleaned_data() returns nothing.
Form returns to page 0 instead of submitting the form and using done()
What I want to happen:
- Data in fields to be retained and displayed when going back, or to the confirmation page
- Form to actually submit and use done() to process and save
The following the my forms.py
class OrderForm(forms.Form):
class Meta:
localized_fields = ('__all__',)
def __init__(self, *args, **kwargs):
self.fields = kwargs.pop('fields')
fields = self.fields
super(OrderForm, self).__init__(*args, **kwargs)
if not isinstance(fields, str):
for i in fields.fields.all():
widget = forms.TextInput()
_type = forms.CharField
if i.field_type == Field.TEXTAREA_FIELD:
widget = forms.Textarea
...
self.fields[i.name] = _type(**fields)
This is supposed to get Database created forms and field data and generate fields accordingly. For example:
Form A has fields:
Name (Regular Text Field)
Address (Textarea)
The above code will then generate fields for those.
The next block of code is from my views.py file
FORM_TEMPLATES = {
"0": 'order/details.html',
"1": 'order/details.html',
"2": 'order/details.html',
"3": 'order/details.html',
"4": 'order/details.html',
"confirm": 'order/confirm.html',
}
class Order(SessionWizardView):
form_list = [OrderForm]
def get_current_step_form(self, company, *args, **kwargs):
step_form = [Form.objects.all()]
step_form.append('Confirm')
return step_form
def get_context_data(self, form, **kwargs):
context = super(Order, self).get_context_data(form=form, **kwargs)
# Returns {}, but I want this to return all previous field values
context.update({
'all_data': self.get_all_cleaned_data(),
})
return context
def post(self, *args, **kwargs):
go_to_step = self.request.POST.get('wizard_goto_step', None)
form = self.get_form(data=self.request.POST)
current_index = self.get_step_index(self.steps.current)
goto_index = self.get_step_index(go_to_step)
if current_index > goto_index:
self.storage.set_step_data(self.steps.current,
self.process_step(form))
self.storage.set_step_files(self.steps.current,
self.process_step_files(form))
return super(Order, self).post(*args, **kwargs)
def get_form(self, step=None, data=None, files=None):
"""
Get the form and add to form_list
"""
form = super(Order, self).get_form(step, data, files)
company = ...
get_forms = self.get_current_step_form(company=company)
form_list_value = dict(self.form_list)['0']
while len(self.form_list.items()) < len(get_forms):
self.form_list.update({str(len(self.form_list.items())): form_list_value})
return form
def done(self, form_list, **kwargs):
return HttpResponse("View")
done() is a work in progress, but it doesn't even seem to reach that point, as it keeps going from (for example) Form 0-1-2-3-0-...
The confirm form will not have any field values form the previous pages and will only return {}
Any help would be appreciated,
Thanks

Related

django admin how to add context when using custom action button?

I have a custom action button "Add post" for each record in my admin panel. I want to save in context obj.id to use it for a default value in admin:channels_post_add form
class ChannelAdmin(admin.ModelAdmin):
list_display = ['title','username', 'invite_link', 'account_actions']
def account_actions(self, obj):
!!!! I WANT TO ADD CONTEXT HERE!!!!
return format_html('<a class="button" href="{}">Add post</a>', reverse('admin:channels_post_add'))
account_actions.short_description = 'Actions'
account_actions.allow_tags = True
class Meta:
model = Channel
admin.site.register(Channel, ChannelAdmin)
class PostAdmin(admin.ModelAdmin):
list_display = ['text', 'media', 'send_type', 'created']
class Meta:
model = Post
def get_form(self, request, obj=None, **kwargs):
form = super(PostAdmin, self).get_form(request, obj, **kwargs)
try:
post_id = !!! AND USE IT HERE!!!!
form.base_fields['channels'].initial = post_id
except Exception as e:
print(e)
return form
You can add GET parameters to the url.
url = "%s?pid=%s" % (reverse(admin:channels_post_add), obj.id)
And then request.GET.get("pid") in get_form():
class ChannelAdmin(admin.ModelAdmin):
...
def account_actions(self, obj):
url = "%s?pid=%s" % (reverse(admin:channels_post_add), obj.id)
return format_html('<a class="button" href="{}">Add post</a>', url)
class PostAdmin(admin.ModelAdmin):
...
def get_form(self, request, obj=None, **kwargs):
form = super(PostAdmin, self).get_form(request, obj, **kwargs)
try:
form.base_fields['channels'].initial = request.GET.get("pid")
except Exception as e:
print(e)
return form
I think you have to use ModelAdmin.get_changeform_initial_data(request)(Django Docs) to set initial value.
A hook for the initial data on admin change forms. By default, fields
are given initial values from GET parameters. For instance,
?name=initial_value will set the name field’s initial value to be
initial_value.
This method should return a dictionary in the form {'fieldname':
'fieldval'}:
url = "%s?channels=%s" % (reverse(admin:channels_post_add), obj.id)
or
def get_changeform_initial_data(self, request):
return {'channels': request.GET.get("pid")}

Initial data for Django Admin inline formset is not saved

I'm trying to pre-fill some inlines in Django Admin with data passed as query params (in case of adding a new object in DB).
class TestCaseInlineFormSet(BaseInlineFormSet):
class Meta:
model = TestCase
fields = '__all__'
def __init__(self, *args, **kwargs):
super(TestCaseInlineFormSet, self).__init__(*args, **kwargs)
ids_string = self.request.GET.get('ids')
if ids_string:
ids = [int(x) for x in ids_string.split(',')]
self.initial = [{'test_case': id} for id in ids]
class TestCaseInline(admin.TabularInline):
model = TestCase
raw_id_fields = ('test_case',)
extra = 1
formset = TestCaseInlineFormSet
def get_formset(self, request, obj=None, **kwargs):
formset = super(TestCaseInline, self).get_formset(request, obj, **kwargs)
formset.request = request
return formset
def get_extra(self, request, obj=None, **kwargs):
extra = super(TestCaseInline, self).get_extra(request, obj, **kwargs)
requested_extras = len(request.GET.get('ids', '').split(','))
return max(extra, requested_extras)
The data is pre-filled fine with this solution, however there's an issue when trying to submit: the pre-filled inlines are not marked as changed, so they're not saved.
I've tried overriding has_changed() on the TestCaseInlineFormSet however it doesn't solve the problem - it seems has_changed() for the formset is never called?
Any idea how to fix this?

Django generic create view default value for model choice field

I have a view that inherits from the generic CreateView and overrides the get_initial method like so:
class PosterVisualCreateView (ModelFormMixin, generic.edit.CreateView, ObjectClassContextMixin):
model = Poster
valid_message = "Successfully created object."
template_name = "poser/create_poster_visual.html"
def get_form_class (self):
return super(PosterVisualCreateView, self).get_form_class(extra="CreateVisual")
def get_form_kwargs (self):
kwargs = super(PosterVisualCreateView, self).get_form_kwargs()
kwargs.update({
"company": self.request.company
})
return kwargs
def get_context_data (self, **kwargs):
context = super(PosterVisualCreateView, self).get_context_data(**kwargs)
context.update({
"company": self.request.company,
})
return context
def get_initial (self):
initial = super(PosterVisualCreateView, self).get_initial()
initial.update({
"company": self.request.company,
"template": self.request.company.template_set.all().first()
})
return initial
def form_valid(self, form):
success_url = super(PosterVisualCreateView, self).form_valid(form)
attributes = form.instance.create_attributes()
for attribute in attributes:
attribute.poster = form.instance
attribute.save()
form.instance.save()
form.instance.render_html(commit=True)
form.instance.save()
return success_url
#method_decorator(login_required)
def dispatch (self, *args, **kwargs):
return super(PosterVisualCreateView, self).dispatch(*args, **kwargs)
The page renders this form:
class PosterFormCreateVisual (CompanyHiddenForm):
"""Create form for Posters."""
template = fields.ModelChoiceField(widget=forms.RadioSelect, queryset=Template.objects.all())
product = fields.ModelChoiceField(widget=forms.Select, queryset=Product.objects.all(),
required=False)
class Meta:
model = Poster
fields = ("template", "product", "company")
def __init__ (self, *args, **kwargs):
company = kwargs.pop("company", None)
assert company is not None, "Company is required to create attribute form."
super(PosterFormCreateVisual, self).__init__(*args, **kwargs)
self.fields["template"].queryset = company.template_set.all()
self.fields["product"].queryset = company.product_set.all()
The initial value should be the first item in the radio selection for template but this isn't the case, can anyone help me out here?
Try this:
"template": self.request.company.template_set.all().first()
But as far as I understand you original code should work too.
BTW, how you tested the form? By hitting the "Refresh" button or Ctrl-R/F5 key? Some browsers reload page but save the previously selected/entered form values. To check initial values you should reload the form page by clicking on the address bar (or pressing Ctrl-L) and then pressing the Enter key.

Edit form not loading a form value

I have an edit view for one of my models.
#login_required
def edit(request, id):
''' Edit form '''
if id:
post = get_object_or_404(Post, pk=id)
if post.user != request.user:
return HttpResponseForbidden()
else:
post = Post()
if request.POST:
form = PostForm(request.POST, instance = post)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('posts_manage'))
else:
form = PostForm(instance = post)
return render_to_response('posts/add.html', {'form':form}, context_instance=RequestContext(request))
Everything works fine, all the post information is loaded correctly, but one of the fields, which is a select box, is not being selected with the value obtained from the DB. Other select boxes are selected to the appropriate value.
The field that is not being populated properly in the model definition:
class Post(models.Model):
...
BATHROOM_CHOICES = ((1,'1'),(1.5,'1.5'),(2,'2'),(2.5,'2.5'),(3,'3'),(3.5,'3.5'),(4,'4'), (4.5,'4.5'),(5,'5+'))
bathrooms = models.DecimalField(max_digits = 2,decimal_places = 1,choices = BATHROOM_CHOICES)
Relevant section inside add.html:
{{ form.bathrooms|bootstrap}}
forms.py
class PostForm(ModelForm):
class Meta:
model = Post
exclude = ('available','user',)
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
return super(PostForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
kwargs['commit'] = False
obj = super(PostForm, self).save(*args, **kwargs)
if self.request:
obj.user = self.request.user
obj.save()
return obj
The data in the DB is not being matched by a choice in BATHROOM_CHOICES
BATHROOM_CHOICES = ((1,'1'),(1.5,'1.5'),(2,'2'),(2.5,'2.5'),(3,'3'),(3.5,'3.5'),(4,'4'), (4.5,'4.5'),(5,'5+'))
and
models.DecimalField(max_digits = 2,decimal_places = 1,
are contradicting.
Your model definition expects all values will have a decimal place of at least 1, and probably coerces values like whole number from 1 to 1.0 in the DB (depending on adapter implementation).
so then when it looks for a choice matching the value 1 !== 1.0 and so no value is selected.
Possible fix:
BATHROOM_CHOICES = ((1.0,'1'),(1.5,'1.5'),(2.0,'2'),(2.5,'2.5'),(3.0,'3'),(3.5,'3.5'),(4.0,'4'), (4.5,'4.5'),(5.0,'5+'))

Django ModelForm with field overwrite does not post data

So, I have the following form:
class DesignItemForm (forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DesignItemForm, self).__init__(*args, **kwargs)
CHOICES=[(i,i) for i in range(MAX_DESIGN_ITEM_QUANTITY)]
self.fields['quantity'] = forms.ChoiceField(choices=CHOICES)
class Meta:
model = DesignItem
fields = ('quantity','trackable',)
My view:
d = Design.object.get(slug=fromInput)
....
DesignItemInlineFormSet = inlineformset_factory(Design, DesignItem, fk_name="design", form=DesignItemForm,)
if request.method == "POST":
formset = DesignItemInlineFormSet(request.POST, request.FILES, instance=d)
if formset.is_valid():
formset.save()
DesignItemInlineFormSet(instance=d)
As you can tell, in my form, I overwrote the quantity field to be a drop down instead of an integer field.
For some reason, when I submit the form, the data is not updated in the database. However, if I change the form to the following, it works (of course, it doesn't have the dropdowns I want, but it posts to the db). Why is this, and how do I fix it?
class DesignItemForm (forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DesignItemForm, self).__init__(*args, **kwargs)
# CHOICES=[(i,i) for i in range(MAX_DESIGN_ITEM_QUANTITY)]
# self.fields['quantity'] = forms.ChoiceField(choices=CHOICES)
class Meta:
model = DesignItem
fields = ('quantity','trackable',)
EDIT: Here is the DesignItem model:
class DesignItem(models.Model):
"""Specifies how many of an item are in a design."""
design = models.ForeignKey(Design, related_name="items")
quantity = models.PositiveIntegerField(default=1)
trackable = models.ForeignKey(Trackable, related_name="used")
have you tried just overriding the widget instead of the whole field?
i guess you want a select widget
def __init__(self, *args, **kwargs):
super(DesignItemForm, self).__init__(*args, **kwargs)
CHOICES=[(i,i) for i in range(MAX_DESIGN_ITEM_QUANTITY)]
self.fields['quantity'].widget = forms.Select(choices=CHOICES)