I have a booking model and a vehicle model.
I want to filter the FK model vehicle, based on the choiceField and keeping the current vehicle item.
MODELS.PY
class Vehicle(models.Model):
STATUS_CHOICES = (
('active', 'active'),
('available', 'available')
)
serial_number = models.CharField(max_length= 10)
status = models.CharField(max_length= 10, choices= STATUS_CHOICES)
class Booking(models.Model):
title = models.CharField(max_length= 50)
vehicle = models.ForeignKey(Vehicle, on_delete.....)
FORMS.PY
class BookingForm(forms.ModelForm):
class Meta:
model= Booking
fields = '__all__'
def __init__(self, *args, **kwargs):
super(BookingForm, self).__init__(*args, **kwargs)
self.fields['vehicle'].queryset = Vehicle.objects.filter(Q(status = 'Available'))
now when I add a vehicle to booking I get all the available vehicles, once a vehicle is added to booking the vehicle status changes from available to active.
when I edit the same entry the vehicle objects disappears as its an active vehicle…
how would I get the filtered query + the current vehicle ???