python - is there any method available in django models, so that i can access its fieldname using string? -
i want 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 name of field want update. lets assume 'address'. how can access attribute, (ie emp_info.address) can update (ie emp_info.address=f_value) using emp_info.save() function.
is there method available other emp_info.address, can access field name using string (ie emp_info[f_name]=f_value ) or something??
you use getattr baked python
attr_name = 'employer_id' if getattr(employee, attr_name) == 3: ...
Comments
Post a Comment