Filtering ModelChoiceField for InlineFormset

8 kali dilihat
Langsung ke pesan pertama yang belum dibaca

IWilliams

belum dibaca,
26 Sep 2019, 06.51.0926/09/19
kepadadjango...@googlegroups.com
Hi,
I would like to use ModelChoiceField in the following ways:

models.py

class Project(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, null=True, blank=True)
...
class Profile(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
...
class FlowGroup(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE,
null=True, blank=True)
class BasicFlow(models.Model):
basicflGroup = models.ForeignKey(FlowGroup, on_delete=models.CASCADE,
null=True, blank=True)
...

class AltFlowGroup(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE,
null=True, blank=True)
referenceBFlow = models.ForeignKey('BasicFlow',
on_delete=models.CASCADE, null=True, blank=True)
class AltFlow(models.Model):
altflGroup = models.ForeignKey(AltFlowGroup, on_delete=models.CASCADE,
null=True, blank=True)
...

forms.py

class AltFlowGroupForm(ModelForm):
class meta:
model = AltFlowGroup
referenceBFlow= forms.ModelChoiceField(label='Success or Abort',
widget=forms.Select,
queryset=BasicFlow.objects.all())


views.py

class ModelUCUpdateView(UpdateView):
model = Profile
form_class = ProfileForm

def get_object(self):
...

def get_success_url(self):
....
def get_context_data(self, **kwargs):
context = super(ModelUCUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['form'] = ProfileForm(self.request.POST,
instance=self.object)
context['basicflowgroup_form'] =
BasicGroupInlineFormSet(self.request.POST, self.request.FILES,

instance=self.object, prefix='modelbasicflowgroup')
context['alflowgroup_form'] =
AltGroupInlineFormSet(self.request.POST,
self.request.FILES,instance=self.object, prefix='altflowgroup')

else:
ccontext['form'] = ProfileForm( instance=self.object)
context['basicflowgroup_form'] =
BasicGroupInlineFormSet(self.request.FILES,

instance=self.object, prefix='modelbasicflowgroup')
context['alflowgroup_form'] =
AltGroupInlineFormSet(self.request.FILES,instance=self.object,
prefix='altflowgroup')
return context

#other methods

In my forms.py, I want the referenceBFlow with the modelchoicefield to only
have the related BasicFlow.Currently, all the BasicFlows are being returned
when I used *queryset=BasicFlow.objects.all()*. I would like to know how to
filter the query dynamically. For instance:
If I have a profile (prof1) and basicGroup (bg1), basicflow (bf1, bf2,
bf3...), AltGroup (ag1). Now, in my template, if I click on the dropdown
for the referenceBFlow for ag1, it should have basicflow (bf1, bf2, bf3...).
If another profile, profile (prof2) and basicGroup (bg2), basicflow (bf4,
bf5, bf6...), AltGroup (ag2), the referenceBFlow for ag2 should have
basicflow (bf4, bf5, bf6...).














--
Sent from: http://python.6.x6.nabble.com/django-users-f58536.html
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru