django多态更新表单集queryset'owner_ptr'问题

问题描述

问题:copyrightApplication实例所有者更新表单集查询集传递问题

我要更新所有属于copyrightAplication实例的所有者

在/ accounts / copyright / secondpart / 2020111101 / update /中的

FieldError 将关键字“ owner_ptr”解析为字段。选择包括:地址, 应用程序,组成,created_at,名称,电子邮件,ID, 研究所,研究所地址,研究所_电子邮件,研究所_手机, 机构名称_孟加拉语,机构名称_英语,is_active, 备忘录,手机,name_in_bangla,name_in_english,国籍,nid, nid_number,所有者比例,护照编号,人,照片, polymorphic_ctype,polymorphic_ctype_id,social_security_number, tin_certificate,Trade_licence,updated_at

models.py

class Owner(polymorphicModel):
    """ copyright application Owner model """
    # person type owner attributes
    name_in_english = models.CharField(
        help_text='Name in English',max_length=100,null=True,blank=True
    )
    name_in_bangla = models.CharField(
        help_text='Name in Bangla',blank=True
    )
    designation = models.CharField(
        help_text='Designation',blank=True
    )
    mobile = models.CharField(
        help_text='Mobile Number',max_length=16,blank=True
    )
    email = models.EmailField(
        help_text='Email Address',blank=True
    )
    nationality = models.CharField(max_length=100,blank=True)

    is_active = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_Now_add=True)
    updated_at = models.DateTimeField(auto_Now=True)

 class Person(Owner):
    """ Person Type copyright Owner """
    pass

    def __str__(self):
        return str(self.name_in_english)


class Institute(Owner):
    """
    Person Type copyright Owner
    ForeginKey/ManyToOne Relation with Owner Model
    """
    pass

    def __str__(self):
        return str(self.institute_name_in_english)


class copyrightApplication(models.Model):
    """
    copyright Application main model
    """
    owner = models.ManyToManyField(
        Owner,blank=True,help_text='Please Select or Create',related_name='applications'
    )

views.py

class copyrightStepTwoCreateView(LoginrequiredMixin,SuccessMessageMixin,UpdateView):
    ''' copyright part create or update '''
    template_name = 'accounts/copyright_parttwo_create_update.html'
    model = copyrightApplication
    form_class = copyrightApplicationDetail
    success_message = "Application was updated successfully"

    def get_success_url(self):
        return reverse("user_dashboard")

    def get(self,request,*args,**kwargs):
        """
        Handles GET requests and instantiates blank versions of the form
        and its inline formsets.
        """
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)

        # application owners
        owners = self.object.owner.all()
        person_type_owners = owners.instance_of(Person)
        institute_type_owners = owners.instance_of(Institute)

        person_fromset = PersonFromset(
            queryset=person_objects,# prblem is here ---------

            prefix='person'
        )
        institute_fromset = InstituteFromset(
            queryset=Owner.objects.none(),prefix='institute'
        )

        return self.render_to_response(self.get_context_data(
            form=form,person_fromset=person_fromset,institute_fromset=institute_fromset
        ))

    def post(self,**kwargs):
        """
        Handles POST requests,instantiating a form instance and its inline
        formsets with the passed POST variables and then checking them for
        validity.
        """
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)

        # owners
        person_fromset = PersonFromset(self.request.POST,self.request.FILES,prefix='person')
        institute_fromset = InstituteFromset(self.request.POST,prefix='institute')

        if (
            form.is_valid() and person_fromset.is_valid()
            and institute_fromset.is_valid()
        ):
            return self.form_valid(form,person_fromset,institute_fromset)
        else:
            return self.form_invalid(form,institute_fromset)

    def form_valid(self,form,institute_fromset):
        """
        Called if all forms are valid. Creates a Recipe instance along with
        associated Ingredients and Instructions and then redirects to a
        success page.
        """
        self.object = form.save()

        # person type owner forms
        person_forms = person_fromset.save(commit=False)
        for person in person_forms:
            if person != {}:
                person.save()
                self.object.owner.add(person)
                self.object.save()

        # institute type owner forms
        institute_froms = institute_fromset.save(commit=False)
        for institute in institute_froms:
            if institute != {}:
                institute.save()
                self.object.owner.add(institute)
                self.object.save()

        # ingredient_form.instance = self.object
        # ingredient_form.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self,institute_fromset):
        """
        Called if a form is invalid. Re-renders the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(
            self.get_context_data(
                form=form,institute_fromset=institute_fromset
            ))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...