'tuple' 对象在 UpdateView 上没有属性 'ordered'

'tuple' object has no attribute 'ordered' on UpdateView

它给了我这个错误,我一生都无法理解为什么:

我有一个从 django-allauth SignupForm 扩展的表单...

class LearnerSignupForm(SignupForm):
    first_name = forms.CharField(max_length=40, required=True)
    last_name = forms.CharField(max_length=40, required=True)

在 allauth 注册视图中调用了表单...

class LearnerSignupView(SignupView):
    form_class = LearnerSignupForm
    success_url = reverse_lazy('users:redirect_profile_mixin')

视图重定向到一个 mixin(它扩展了从请求中获取经过身份验证的用户的 RedirectView,并将连接重定向到一个传递参数 user.id..

的 UpdateView
class LearnerUpdateView(UpdateView):
    model = User
    form_class = UserForm
    formset_Class = LearnerFormSet
    template_name = "formset_edit_learner.html"
    success_url = "home"

    def get_context_data(self, **kwargs):
        context = super(LearnerUpdateView, self).get_context_data(**kwargs)
        qs = Learner.objects.get_or_create(user=self.request.user)
        formset = LearnerFormSet(queryset=qs)
        context["learner_formset"] = formset
        return context

然后终于到了UpdateView。正是在这一点上,我得到了错误

'tuple' object has no attribute 'ordered'

Exception Location: C:\Users\aless.virtualenvs\hs_03-LQeWV4ia\lib\site-packages\django\forms\models.py, line 639, in get_queryset

我知道我可能在做一些非常愚蠢的事情,但我就是看不到。我提前感谢谁能提供一些建议!

正如 acg 所说,您正在制作一个元组:https://docs.djangoproject.com/en/3.1/ref/models/querysets/#get-or-create

Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

因此,在不更改代码的情况下,您应该这样做:

qs = Learner.objects.get_or_create(user=self.request.user)
formset = LearnerFormSet(queryset=qs[0])

但这不是很漂亮,你最好这样做:

qs, created = Learner.objects.get_or_create(user=self.request.user)
formset = LearnerFormSet(queryset=qs)