Django自动完成初始数据

时间:2014-10-20 17:35:28

标签: django django-forms django-autocomplete-light

我有一个表格,Django自动完成灯工作并保存良好。但是,我也有"Edit"的链接。然后我使用他们保存的初始数据预先填充表单,除了在编辑模式下没有显示Django自动完成光场的数据

这是我的编辑表单方法:

@login_required(login_url='/')
def edit_credits(request, template_name):
    user = request.user
    credits_profile = get_object_or_404(CreditOptions, user=user)
    credits = CreditOptions.objects.filter(user=user)
    credits_data = list(credits.values())
    if credits_profile.user != user:
        return HttpResponseForbidden("Sorry, something here doesn't make sense. Please try again.")

    if request.method == 'POST':
        formset = OptionFormset(request.POST, instance=credits_profile)
        if formset.is_valid():
            for form in formset.forms:
                instance = form.save(commit=False)
                instance.user = user
                instance.save()
            messages.success(request, "Successfully updated your credits.")
            return redirect(urlresolvers.reverse('skills'))
    else:
        formset = OptionFormset(initial=credits_data)
    return render(request, template_name, {
        'formset': formset,
    })

这是我的forms.py

class CreditOptionsForm(forms.ModelForm):
    crew_position = forms.ModelChoiceField(CrewPosition.objects.all(), 
        widget=autocomplete_light.ChoiceWidget('CrewPositionAutocomplete'), 
        required=False, 
        help_text='Start typing and it will auto-suggest for you. e.g: PRODUCER')

    class Meta:
        model = CreditOptions
        exclude = ('user',)

OptionFormset = formset_factory(CreditOptionsForm, extra=1)

我想要的初始数据是这样的: initial data desired

通过这种方式,他们可以“x”出数据并替换它。

有什么建议吗?

谢谢!

0 个答案:

没有答案