Beispiel #1
0
def modelUpdate(request):	
	print "Creation page"
	m_tb_name = request.session['model_table']
	print 'model_classes_field', m_tb_name
	model_class = get_model('Directories', m_tb_name)
	model_name = model_class.__name__
	field_names = model_class._meta.get_all_field_names()			
	if request.method == 'POST': 
		print "m_tb_name is: ", m_tb_name
		form_class = get_dynamic_form(m_tb_name)
		form = form_class(request.POST)			
		if form.is_valid(): # All validation rules pass
			print "form is valid!" 
			row = form.save() 
			form = dbForm()
			#send a message to inform users that form submission was a success
			messages.success(request, 'New model data created!')
			return HttpResponseRedirect(reverse('Directories:index'))
		else:
			#will probably return the form with errors
			return render(request, 'Directories/create.html', {'form':form, 'model_name':model_name, 'field_names':field_names})
		
	else:
		form_class = get_dynamic_form(m_tb_name)
		form = form_class() # An unbound form
		print "no form submission: ", form.errors
	return render(request, 'Directories/create.html', {'form':form, 'model_name':model_name, 'field_names':field_names})
Beispiel #2
0
def modelEdit(request):
	print "Edit page"
	#create a global list with the instances to be updated. This will be called in the modelEdit view
	mvar = request.session['model_table']
	model_class = get_model('Directories', mvar)
	# create querysets using the field chosen in the list template
	update_items = request.session['u_list']
	field_names = request.session['field_list']
	# Get the field values list from dlist as shown below and then show the values in the edit template, inside the textboxes
	f_val_list = sorted(field_values.items()) #sort puts them in alphabetical order but you may need to change this for other models
	form_class = get_dynamic_form(mvar)
	if "_alter" in request.POST:
		#print "instance: ", t
		print "alter presseedddd!"
		form = form_class(request.POST)#, instance=t)
		if form.is_valid(): # All validation rules pass
			#print "instance: ", t
			print "form valid! now start updating fields!" 
			# use the form data to change the queryset 't' field values
			print 'start forloop', len(field_names)
			# iterate over update_list to get the pks from sent from the list template form
			for count in range(0, len(update_items)):
				for item in update_items:
					t = model_class.objects.get(pk=item[count])
					print "instance: ", t
			# iterate over field_names to get the user form input for each field in field_names
			for count in range(1, len(field_names)):
				form_data = form.cleaned_data[field_names[count]]
				data = form_data.encode("utf8")
				print "FORM DATA:", data
				print "field_name: ", field_names[count]
				setattr(t, field_names[count], form_data)
				print "data", getattr(t, field_names[count]).encode("utf8")
			#print 'end forloop'
			##### alles epiloges gia to t:
			##### Foo.objects.get(pk=????).update(**data) opou data = {'field1': 'value1', 'field5': 'value5', 'field7': 'value7'}
			##### i kanw Foo.objects.get(pk=????).update(**{field_list[2]:item})
			# after changing the queryset data you can now save to database
			t.save()
			#send a message to inform users that form submission was a success
			messages.success(request, 'Selected fields updated') # kalw ama kanei duplicate la8os na epistrefei validate_unique()
			return HttpResponseRedirect(reverse('Directories:index')) 
		else:
			# will return the form with an error message
			return render(request, 'Directories/edit.html', {'form':form, 'field_list':field_list, 'f_val_list':f_val_list})
	else:
		for item in update_list:
			t = model_class.objects.get(**{field_list[0]:item[0] for item in update_list})
		form = form_class(instance=t)
		print "something not working or submit button not pressed"
	return render(request, 'Directories/edit.html', {'form':form, 'field_list':field_list, 'f_val_list':f_val_list})