我有以下型号:
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
我有一个模型表单,我尝试给出project_form的名称,类和id:
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)})"
这通过ajax.py包含:
@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()
我对此没有任何问题。我的问题是在返回dajax.json()之前访问表单,以便从中检索值。我尝试过form.cleaned_data ['territory']以及form.cleaned_data.get('territory')。我已经将问题跟踪到通过表单传递到视图中的表单,因为它似乎是空白的,所以
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)})"
似乎无法正常工作
我想要做的就是检索一个字段的值,而不是实际传递整个表单,但这是我似乎能够获得我想要的数据的唯一方法。还有更好的方法吗?