My ModelChoiceField is not validating saying that the selected choice is not an instance.
I am passing a custom queryset to my modelChoiceField that uses a custom manager (objects_sys) to return extra entries that are archived and do not normally display with normal queries. The entries are displayed and I can select them, but when the form posts and Django tries to validate, it must be using another query that filters out the archived items so those are not valid choices.
I'm using class based views - this vails in form_valid()
Models:
class Item(models.Model):
name = UpperCaseCharField(max_length=10, primary_key=True)
qty_on_hand = models.IntegerField()
description = models.CharField(max_length=200)
archive = models.BooleanField()
class ItemLoc(models.Model):
item = models.ForeignKey(Item, on_delete=models.PROTECT)
location = models.CharField(max_length=200)
My Form:
class ItemForm(forms.ModelForm):
class Meta:
model = ItemLoc
fields = (
'item',
'location'
)
def __init__(self, *args, **kwargs):
super(ItemForm, self).__init__(*args, **kwargs)
self.fields['item'].queryset = Item.objects_sys.filter(qty_on_hand__gt=0)
Specifically, there is an existing entry in the Item table with name = "MULTIPLE" that has its archive field = True. The objects manager returns only active records (archive=False) The objects_sys manager returns all records including archived records (this was the original objects manager). The error returned after attempted validation is: "Item instance with name 'MULTIPLE' does not exist."
Do I need to pass this custom query in again to the ModelChoiceField at some point (maybe during POST) so that the extra values are available for validation?