CreateView 中的重复查询
Duplicated queries in CreateView
我有一个包含两个相关查询集的 CreateView,但为了我的 class 工作,我重复了这些查询。
我试过这个:
class CrearEvidencia(CreateView):
from django.shortcuts import get_object_or_404, get_list_or_404
from core.models import Pipol
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
pipol = Pipol.objects.filter(puesto=meta.puesto)
def get_form(self, form_class):
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = self.pipol
return form
def get_initial(self):
meta = self.meta
return {'meta': meta, }
但是没有用。我收到此错误:
NameError at /metas/4/add
name 'self' is not defined
为了我的class作品,我这样写:
class CrearEvidencia(CreateView):
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
def get_form(self, form_class):
from django.shortcuts import get_object_or_404, get_list_or_404
from core.models import Pipol
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = Pipol.objects.filter(puesto=meta.puesto)
return form
def get_initial(self):
from django.shortcuts import get_object_or_404
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
return {'meta': meta, }
如何避免重复查询?
重复查询可以python属性:
@property
def meta(self):
return get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
@property
def pipol(self):
return Pipol.objects.filter(puesto=self.meta.puesto)
感谢@lukkol
此外,我删除了 get_initial()
以仅保留一个功能,所以现在是 class:
class CrearEvidencia(CreateView):
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
@property
def meta(self):
return get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
@property
def pipol(self):
return Pipol.objects.filter(puesto=self.meta.puesto)
def get_form(self, form_class):
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = self.pipol
form.fields['meta'].initial = self.meta
return form
我有一个包含两个相关查询集的 CreateView,但为了我的 class 工作,我重复了这些查询。
我试过这个:
class CrearEvidencia(CreateView):
from django.shortcuts import get_object_or_404, get_list_or_404
from core.models import Pipol
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
pipol = Pipol.objects.filter(puesto=meta.puesto)
def get_form(self, form_class):
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = self.pipol
return form
def get_initial(self):
meta = self.meta
return {'meta': meta, }
但是没有用。我收到此错误:
NameError at /metas/4/add
name 'self' is not defined
为了我的class作品,我这样写:
class CrearEvidencia(CreateView):
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
def get_form(self, form_class):
from django.shortcuts import get_object_or_404, get_list_or_404
from core.models import Pipol
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = Pipol.objects.filter(puesto=meta.puesto)
return form
def get_initial(self):
from django.shortcuts import get_object_or_404
meta = get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
return {'meta': meta, }
如何避免重复查询?
重复查询可以python属性:
@property
def meta(self):
return get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
@property
def pipol(self):
return Pipol.objects.filter(puesto=self.meta.puesto)
感谢@lukkol
此外,我删除了 get_initial()
以仅保留一个功能,所以现在是 class:
class CrearEvidencia(CreateView):
model = Evidencia
form_class = EvidenciaForm
template_name_suffix = '_add'
@property
def meta(self):
return get_object_or_404(MetasSPE, pk=self.kwargs['pk'])
@property
def pipol(self):
return Pipol.objects.filter(puesto=self.meta.puesto)
def get_form(self, form_class):
form = super(CrearEvidencia, self).get_form(form_class)
form.fields['miembro'].queryset = self.pipol
form.fields['meta'].initial = self.meta
return form