I have the following model:
class Usage(models.Model):
region = models.ForeignKey(Region)
territory = models.ForeignKey(Territory)
usage_in_month = models.ForeignKey(UsageInMonth)
def __unicode__(self):
return '%s' % self.region
I have a modelform which I tried giving a name, class and id of project_form:
self.fields['usage_in_month'].widget.attrs['onchange'] = "Dajaxice.projects.update_usage_in_month(Dajax.process,{'option':this.value, 'current':this.id, 'form': $('project_form').serialize(true)})"
This goes through the ajax.py which contains:
@dajaxice_register
def update_usage_in_month(request, option, current, form):
dajax = Dajax()
form 开发者_如何学编程= ProjectForm(form)
# As the first ID has no prefix current is shorter
if current[:40] == 'id_usages-tvusage-content_type-object_id':
current_short = current[:42]
current_usage_in_month = '#' + current_short + '-usage_in_month'
current_suggested_rate = '#' + current_short + '-suggested_rate'
else:
# Current is longer for other IDs
current_short = current[:82]
current_usage_in_month = '#' + current_short + '-usage_in_month'
current_suggested_rate = '#' + current_short + '-suggested_rate'
cpa_rate = UsageInMonth.objects.get(id=option).cpa_rate
dajax.assign(current_suggested_rate,'value',cpa_rate)
return dajax.json()
I have no problems with this. My issue is accessing the form prior to the return dajax.json() in order to retrieve a value from it. I have tried form.cleaned_data['territory'] as well as form.cleaned_data.get('territory'). I have tracked down the issue to the form being passed into the view via the form as it seems to be blank, so the
self.fields['usage_in_month'].widget.attrs['onchange'] = "Dajaxice.projects.update_usage_in_month(Dajax.process,{'option':this.value, 'current':this.id, 'form': $('project_form').serialize(true)})"
does not seem to be working
All I am trying to do is retrieve the value of one field not actually pass in the whole form but this is the only way I seem to be able to get the data I want. Is there a better way?
精彩评论