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

如何解决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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?