Hello, I am trying to set the choices in a form used in a Django generic update view to change based on the value of a field in the current object being updated. In forms.py below, I am trying to replace CHOICES with a callable that takes the value of current_status for the object being updated and returns a list of tuples based on the argument it is passed. E.g. if current_status = 1, then I want to the callable to return [(2, Choice 2)]
Is this possible? I am having trouble figuring out how to do this. Thanks! Below are snips from my current code.
from django import forms from .models import BatchRecord CHOICES = [ (1, 'Choice 1'), (2, 'Choice 2'), ] class BatchRecordChangeStatusForm(forms.ModelForm): current_status = forms.ChoiceField(choices=CHOICES) class Meta: model = BatchRecord fields = ['current_status']
class BatchRecordChangeStatusView(UpdateView): model = BatchRecord form_class = BatchRecordChangeStatusForm
urlpatterns = [ path('changestatus/<int:pk>/', BatchRecordChangeStatusView.as_view(), name='changestatus'), ]
class BatchRecord(models.Model): STATUS_CHOICES = ( (1, 'Creation in Progress'), (2, 'Ready for Production'), (3, 'In Production'), (4, 'Tier 1 Review'), (5, 'Tier 2 Review'), (6, 'Complete'), ) current_status = models.IntegerField(choices=STATUS_CHOICES, blank=False, default=1)