python - Django Edit Form Queryset made of Query + Current Selected Option - How to get? -


my forms.py

class createvesselform(forms.modelform): class meta:     model = vessel     exclude = ['active']  # filtering choices def __init__(self, *args, **kwargs):     super(createvesselform, self).__init__(*args, **kwargs)     # filtering free slots     self.fields['slot'].queryset = slot.objects.filter(is_free=true)     # filtering free storages     self.fields['storage'].queryset = storage.objects.filter(is_free=true) 

the slot field foreignkey , storage field manytomany field.

in views.py, time save form change status of "is_free" false. when time edit item(vessel) - getting form instance - options selected before, no longer appear in form fields because queryset filtering status=true.

the perfect form queryset me be:

for foreignkey

the current selected item "vessel.slot" + slot.objects.filter(is_free=true) ?

for manytomany

the current selected item "vessel.storage" + storage.objects.filter(is_free=true) ?

is there way done ?

you can try this:

class createvesselform(forms.modelform):      class meta:         model = vessel         exclude = ['active']      # filtering choices     def __init__(self, *args, **kwargs):         super(createvesselform, self).__init__(*args, **kwargs)         if kwargs['instance']:             self.fields['storage'].queryset = kwargs['instance'].storage.all()|storage.objects.filter(is_free=true)             self.fields['slot'].queryset = slot.objects.filter(pk=kwargs['instance'].slot.pk)|slot.objects.filter(is_free=true) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -