Django CRUD更新对象与用户

时间:2017-04-28 03:26:00

标签: python django crud

我遵循了Vitor Freitas的精彩教程How to Implement CRUD Using Ajax and Json。对于我的项目,我有一个与用户有多对一关系的对象,我希望用户能够添加和更新。我可以添加对象,但是当我尝试更新它时会抛出一个 ValueError: Cannot query "Case object": Must be "User" instance.

views.py

def case_update(request, pk):
    case = get_object_or_404(Case, pk=pk)
    if request.method == 'POST':
        form = CaseForm(request.POST, instance=case)
    else:
        form = CaseForm(instance=case)
    return save_case_form(request, form, 'cases/includes/partial_case_update.html')

当我尝试保存编辑时,它会中断,但我很难看到解决方法。要编辑这种情况,我需要将表单作为此特定情况的实例,因为用户可能有很多情况。当我将实例设置为用户时,没有任何反应,当我完全删除实例时,它显然只是复制了一个案例,所以我有两个相同的案例。

如有必要,我可以发布更多代码。谢谢

修改 我只使用inlineformset重构了我的代码,它正在工作......有点儿。我现在可以编辑案例,但我仍然无法编辑单个案例。当我尝试使用inlineformset执行ValueError: Cannot query "Case object": Must be "User" instance的实例时,我会继续收到case = get_object_or_404(Case, pk=pk)。当我将其更改为用户实例时,它会显示该特定用户的所有情况,但会正确保存。

def save_case_form(request, case_formset, template_name):
    data = dict()

    if request.method == 'POST':
        if case_formset.is_valid():
            case_formset.save()
            data['form_is_valid'] = True
            cases = Case.objects.all()
            data['html_case_list'] = render_to_string('cases/includes/partial_case_list.html', {
                'cases': cases
            })
        else:
            data['form_is_valid'] = False
    context = {'case_formset' : case_formset}
    data['html_form'] = render_to_string(template_name, context, request=request)
    return JsonResponse(data)


def case_create(request):
    if request.method == 'POST':
        case_formset = CaseFormset(request.POST, instance = request.user)
    else:
        case_formset = CaseFormset()
    return save_case_form(request, case_formset, 'cases/includes/partial_case_create.html')


def case_update(request, pk):
    case = get_object_or_404(Case, pk=pk)
    if request.method == 'POST':
        case_formset = CaseFormset(request.POST, instance=request.user)
    else:
        case_formset = CaseFormset(instance=request.user)
    return save_case_form(request, case_formset, 'cases/includes/partial_case_update.html')

forms.py

class CaseForm(forms.ModelForm):
    class Meta:
        model = Case
        fields = ('title', 'publication_date', 'author', 'price', 'pages', 'case_type', )


CaseFormset = inlineformset_factory(User,Case, 
                                    fields = ('title', 'publication_date', 
                                              'author', 'price', 
                                              'pages', 'case_type', ), 
                                    can_delete = False,
                                    extra = 1)

修改

非DRY实施有效:

forms.py

class CaseForm(forms.ModelForm):
    class Meta:
        model = Case
        fields = ('title', 'publication_date', 'author', 'price', 'pages', 'case_type', )


CaseFormset = inlineformset_factory(User,Case, 
                                    fields = ('title', 'publication_date', 
                                              'author', 'price', 
                                              'pages', 'case_type', ), 
                                    can_delete = False,
                                    extra = 1)

CaseFormsetUpdate = inlineformset_factory(User,Case, 
                                    fields = ('title', 'publication_date', 
                                              'author', 'price', 
                                              'pages', 'case_type', ), 
                                    can_delete = False,
                                    extra = 0)

views.py

def save_case_form(request, case_formset, template_name):
    data = dict()

    if request.method == 'POST':
        if case_formset.is_valid():
            case_formset.save()
            data['form_is_valid'] = True
            cases = Case.objects.all()
            data['html_case_list'] = render_to_string('cases/includes/partial_case_list.html', {
                'cases': cases
            })
        else:
            data['form_is_valid'] = False
    context = {'case_formset' : case_formset}
    data['html_form'] = render_to_string(template_name, context, request=request)
    return JsonResponse(data)


def case_create(request):

    if request.method == 'POST':
        case_formset = CaseFormset(request.POST, instance = request.user)
    else:
        case_formset = CaseFormset()
    return save_case_form(request, case_formset, 'cases/includes/partial_case_create.html')


def case_update(request, pk):

    case = get_object_or_404(Case, pk=pk)
    if request.method == 'POST':
        case_formset = CaseFormsetUpdate(request.POST, instance=request.user)
    else:
        case_formset = CaseFormsetUpdate(instance=request.user, queryset = Case.objects.filter(pk=pk))
    return save_case_form(request, case_formset, 'cases/includes/partial_case_update.html')

在视图中实例化时,我无法找到更改内联formset上的额外参数的方法。所以我刚做了第二个,额外设置为0并将其添加到我的更新案例视图中。我仍然对更好的方法感兴趣。

1 个答案:

答案 0 :(得分:0)

经过大量的反复试验后,我找到了解决方案。我一直在使用内联表单集,因为我一直在找到指向该方向的答案,但我宁愿手动执行它,如果可能的话,我一直在尝试,但是我这样做的方式因为某些原因在这个例子中不起作用。

我一直在尝试这样的事情:

case = form.save(commit = False)
case.user = request.user

form = (request.POST, instance = request.user)
form.save()

我遇到了this solution,它现在按预期工作,没有内联表单集。

form.instance.user = request.user
form.save()

这看起来几乎等同于我一直在做的事情,但它起作用了。