diff --git a/care/emr/api/viewsets/questionnaire.py b/care/emr/api/viewsets/questionnaire.py index b256d338fc..1bdd62a58d 100644 --- a/care/emr/api/viewsets/questionnaire.py +++ b/care/emr/api/viewsets/questionnaire.py @@ -61,12 +61,15 @@ def perform_create(self, instance): ) def validate_data(self, instance, model_obj=None): + # If we're editing an existing questionnaire (model_obj is not None) + # and there are no responses linked to this questionnaire yet if ( model_obj - and not QuestionnaireResponse.objects.filter( - questionnaire=model_obj - ).exists() + and QuestionnaireResponse.objects.filter(questionnaire=model_obj).exists() ): + # Prevent editing if the questionnaire has already been used (has responses) + # This ensures data integrity by not allowing changes to questionnaires + # that are actively being used raise ValidationError("Cannot edit an active questionnaire") def authorize_create(self, instance):