表格无效。 ID:选择有效选项。这种选择不是可用的选择之一

时间:2015-08-12 07:16:34

标签: django django-forms django-views django-orm django-managers

当我运行此代码时,即使整个数据都是正确的。 demobilized_formset.is_valid()给了我Falsedemobilized_formset.errors给了{u'id': [u'Select a valid choice. That choice is not one of the available choices.']}

我不知道为什么id {u'id': [u'Select a valid choice. That choice is not one of the available choices.']}错误来自class DemobilizedEmployee(models.Manager): use_for_related_fields = True def get_queryset(self): # When I use filter instead of exclude it works fine. return super(DemobilizedEmployee, self).get_queryset().exclude( demobilized_date=None)

managers.py

class PurchasedEmployees(AuditDeleteSafeModel):
    purchase = models.ForeignKey(Purchase)
    employee = models.ForeignKey(Employee)
    category = models.CharField(max_length=32)
    mobilized_date = models.DateField()
    demobilized_date = models.DateField(null=True, blank=True)
    period_worked = models.PositiveSmallIntegerField(null=True, blank=True)

    mobilized_emp = MobilizedEmployee()
    demobilized_emp= DemobilizedEmployee()
    objects =  # Is also an custom Manager which changes deleted=True in Abstract Model
    class Meta:
        db_table = 'purchased_employees'

models.py

class DetailMobilizeForm(forms.ModelForm):
    class Meta:
        model = PurchasedEmployees
        fields = ('employee', 'category', 'mobilized_date',
                  'demobilized_date', 'period_worked')
DetailMobilizeInlineFormSet = modelformset_factory(
    PurchasedEmployees, can_delete=False, extra=0, form=DetailMobilizeForm)

forms.py

def update_demobilize_emp(request, id=None):
    purchase = get_object_or_404(Purchase, id=id) if id else Purchase()
    demobilized_queryset = PurchasedEmployees.demobilized_emp.filter(
        purchase=purchase)

    if request.POST:
        demobilized_formset = DetailMobilizeInlineFormSet(
            data=request.POST,
            prefix='demobilized_form')

        if demobilized_formset.is_valid():
            demobilized_formset.save()
            status_code, res = 200, {'status': 'true'}
        else:
            for form in demobilized_formset:
                import ipdb; ipdb.set_trace()
            errors = {}
            errors[demobilized_formset.prefix + '_errors'] =\
                demobilized_formset.errors
            status_code, res = 202, {'error': errors , 'status': 'false'}

        return HttpResponse(
            json.dumps(res), content_type='application/json',
            status=status_code)

    else:
        demobilized_formset = DetailMobilizeInlineFormSet(
            queryset=demobilized_queryset,
            prefix='demobilized_form')

    return render_to_response(
        'purchase/update_demobilized_employees.html', {
        'demobilized_formset': demobilized_formset, 'purchase': purchase},
        context_instance=RequestContext(request))

views.py

<div class="field">
    {{ demobilized_formset.management_form }}
    <div class="box">
      {% for forms in demobilized_formset.forms %}
      <div class="box_item">
          {{ forms }}
          <br class="clear">
      </div>
      {% endfor %}
  </div>

模板

...

<label for="id_demobilized_form-0-period_worked">Period worked:</label>
<input id="id_demobilized_form-0-period_worked" type="number" value="100" name="demobilized_form-0-period_worked" min="0">
<input id="id_demobilized_form-0-id" type="hidden" value="8" name="demobilized_form-0-id">
<ul class="errorlist">
    <li>Select a valid choice. That choice is not one of the available choices.</li>
</ul>

并且

$preExecutionCommand = "dir D:\Test"
$preExecutionArgs = $preExecutionCommand -split '\s+'
$preExecutionCmd = $preExecutionArgs[0]
$preExecutionNumArgs = $preExecutionArgs.Length - 1
if ($preExecutionNumArgs -gt 0) {
  $preExecutionArgs = $preExecutionArgs[1..$preExecutionNumArgs]
  & $preExecutionCmd $preExecutionArgs
} else {
  & $preExecutionCmd
}

0 个答案:

没有答案