i want to update fields using ajax:
models.py
class EmployerInfo(models.Model):
employer = models.ForeignKey(Employer, unique=True)
address=models.CharField(max_length=100, blank=True)
city=models.CharField(max_length=40, blank=True)
contactinfo.html
<form id="ajax-form">
<fieldset>
<legend>Contact Information</legend>
<label>Address:</label>
<input type="text" id="address" value="{{ empinfo.address }}" />
<label>City:</label>
<input type="text" id="city" value="{{ empinfo.city }}" /> <i class="icon-ok"></i>
</fieldset>
</form>
<script>
$(document).ready(function()
{
$("form#ajax-form").find(":input").change(function()
{
var field_name=$(this).attr("id");
var field_val=$(this).val();
var params ={ param1:field_name, param2:field_val };
$.ajax({ url: "/employer/contactinfo/save/",
dataType: "json",
data: params,
success: setResult
});
});
});
urls.py
.....other urls
url(r'^employer/contactinfo/save/$', 'save_employer_info_ajax'),
view.py
def save_employer_info_ajax(request):
emp=Employer.objects.get(user=request.user)
emp_info=EmployerInfo.objects.get(employer=emp)
f_name=request.GET['param1']
f_value=request.GET['param2']
return HttpResponse(json.dumps({"issuccess": 'yes'}), content_type="application/json")
f_name the name of the field i want to update. lets assume it be 'address'. how can i access that attribute, (ie emp_info.address) so that i can update (ie emp_info.address=f_value) using emp_info.save() function.
is there any method available other than emp_info.address, so that i can access the field name using string (ie emp_info[f_name]=f_value ) or something??