Juan Carlos Asuncion February 2016

Django: get_readonly_fields does not return correctly

I would like to ask some assistance in this matter, I would like to set the fields in the admin readonly via get_readonly_fields depending on the model's APP_STATUS

my code:

def get_readonly_fields(self, request, obj=None):
    if obj.APP_STATUS != 1 :
        return self.readonly_fields + ['PRE_FINAL_INTERVIEW_EXAM_SCORE', 'PRE_FINAL_INTERVIEW_EXAM_SCORE_STATUS', 'COMMENT_ON_INITIAL_EVALUATION']
    elif obj.APP_STATUS != 4 :
        return self.readonly_fields + ['POST_TRAINING_EVALUATION_EXAM_SCORE', 'POST_TRAINING_EVALUATION_EXAM_SCORE_STATUS', 'COMMENT_ON_TRAINING_EVALUATION']
    return self.readonly_fields

the problem is that only the first if is being rendered as read only, what am I doing wrong? Thanks in advance.

Answers


doru February 2016

It can't be done the if branching using != because the conditions doesn't exclude each other: let's say obj.APP_STATUS = 2, this validates both conditions if obj.APP_STATUS != 1 and if obj.APP_STATUS != 4 and whichever is first that will be run.

Post Status

Asked in February 2016
Viewed 2,400 times
Voted 14
Answered 1 times

Search




Leave an answer