1

当用户单击按钮时,我试图在模式框中显示与另一个模型相关的所有模型。但是,更新视图无法按我希望的方式工作。我收到与基于类的视图(模型对象更新视图)相关的 URL 模式的未找到,而不是 200 OK

我尝试了几种方法,但下面是最新的一种。当我打印出 get_queryset (我已经覆盖)时,我得到了对象。我试图打印上下文 ['formset'],但似乎代码没有达到那么远。

我正在使用django-bootstrap-modal-forms 1.4.2来显示模型表单集,我正在使用django-polymorphic 2.1.2来处理模型的多态性。我使用这种方法的原因是: 1. 模型流和模型对象之间存在多对多的关系。2. 使用模态框可以使界面更简洁,而不是在同一页面上同时拥有模型流和模型对象。3. 我想让多态方法处理与模型流相关的对象。

models.py - 有两个主要模型是多态模型 ModelFlow 和 ModelObject 以及它们的子类。

class ModelFlow(PolymorphicModel):
    decription = models.CharField(max_length=250)
    relatedModelObject = models.ManyToManyField('ModelObject', through='ModelFlowObject', null=True, blank=True)

class ModelBFlow(ModelFlow):
    modelflGroup = models.ForeignKey(BasicFlowGroup, on_delete=models.CASCADE, null=True, blank=True)


class ModelAFlow(ModelFlow):
    modelflGroup = models.ForeignKey(BoundedFlowGroup, on_delete=models.CASCADE, null=True, blank=True)


class ModelObject(PolymorphicModel):
    objectName = models.CharField(max_length=250)

class ModelFlowObject(PolymorphicModel):
    flowModel = models.ForeignKey(ModelFlow, on_delete=models.CASCADE)
    objectModel = models.ForeignKey(ModelObject, on_delete=models.CASCADE)

class WebPageObject(ModelObject):
    objectValue = models.CharField(max_length=3, choices=WEBPAGEVALUE_CHOICES)

class LinkObject(ModelObject):
    paramType = models.CharField(max_length=3, choices=WEBPAGEVALUE_CHOICES)
    linkType = models.CharField(max_length=3, choices=WEBPAGEVALUE_CHOICES)

forms.py - 我创建了基于 WebPageObject 和 LinkObject 的两个表单,它们都继承自 ModelObject(多态模型),然后我创建了一个多态模型表单集。

class ModelWebPageObjectForm(ModelForm):
      class Meta:
        model = WebPageObject
        fields='__all__'

class ModelLinkObjectForm(ModelForm):
    class Meta:
        model = LinkObject
        fields = '__all__'

Oformset = polymorphic_modelformset_factory(ModelObject,formset_children=(                                                      
PolymorphicFormSetChild(WebPageObject, form=ModelWebPageObjectForm, fields = '__all__'),), fields='__all__',extra=1)
PolymorphicFormSetChild(LinkObject, form=ModelLinkObjectForm, fields = '__all__'),

views.py - 在这个视图中,我使用了 ModelObject(一个多态模型)作为更新视图的模型。然后我创建了一个带有模型/表单对的 form_class

class DisplayModelFlow(Updateview):
   ....
   #this view is already working. It displays all the modelflow that is related to another model. Here I am using inlineformset. 

class ModelObjectUpdateView(BSModalUpdateView):
    #in this view the self.kwargs['pkObj'] from the urls.py will be used to get the modelflow object. Next, modelflow is used to find the all the modelobject (webpage or link) that are related to this. So, I am using a polymorphic modelformset to display all the modelobjects in the modal box.
    model=ModelObject
    form_class = {WebPageObject:ModelWebPageObjectForm,
                  LinkObject:ModelLinkObjectForm,
    }
    template_name = 'modalPopUp/updateModelObject.html'
    success_message = 'Success: Object(s) Updated.'

    def get_form_class(self):
        return self.form_class[self.object.__class__]

    '''def get_object(self):
        flowmodel=ModelFlow.objects.filter(id=self.kwargs['pkObj'])
        return flowmodel[0]'''

    def get_queryset(self):
        flowmodel=ModelFlow.objects.filter(id=self.kwargs['pkObj'])
        myobj=flowmodel[0].relatedModelObject.all()
        return myobj

    def get_context_data(self, **kwargs):
        context = super(ModelObjectUpdateView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['formset'] = Oformset(queryset=self.get_queryset())
        else:
            context['formset'] = Oformset(queryset=self.get_queryset())
        return context

    def post(self, request, *args, **kwargs):
        formset = Oformset(self.request.POST)
        if (formset.is_valid()):
            return self.form_valid(formset)
        else:
            return self.form_invalid(formset)

    def form_valid(self, formset):
        self.object = formset.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, formset):
        return self.render_to_response(self.get_context_data(formset=formset))

    def get_success_url(self):
        self.success_url = reverse_lazy('recSys:FlowRecSysList', kwargs={'project': self.kwargs['project'],
                                                                         'useCaseName': self.kwargs['useCasename']})
        return self.success_url

urls.py - 这是更新视图获取的 url。

...
path('<str:project>/UCModelUpdate/<int:pk>/updateModelObject/<str:pkObj>', ModelObjectUpdateView.as_view(), name='updateModelObject'),
...

模板

There is a .html to display the modelFlow along with a button that when is clicked displays the modal box for the objects will be displayed. There may be several modelFlows on the .html along with a button for the modal box that should display only the modelobjects that are related to it.

预期的结果应该是一个模态框,其中所有允许基于上下文 ['formset'] 中的查询集。此外,GET 应该是 200 OK。

但是,我收到此消息: Not Found: /useCaseExtract/Marvel-E-Store/UCModelUpdate/2/updateModelObject/319/ [15/Sep/2019 12:26:41] "GET /useCaseExtract/Marvel-E-Store/UCModelUpdate/2/updateModelObject/319/ HTTP/1.1" 404 5459 并且没有显示模式框。

4

0 回答 0