Reputation: 388
I wrote a signal that changes a field inside user model. it works fine but when a user registers for the first time it gives me this error:
UnboundLocalError: local variable 'scientific' referenced before assignment
this is my signal:
@receiver(pre_save, sender=User, dispatch_uid='progress_change')
def progress(sender, instance, **kwargs):
user = instance
if ScientificInfo.objects.filter(user=user).exists():
scientific = ScientificInfo.objects.get(user=user)
if ReligiousInfo.objects.filter(user=user).exists():
religious = ReligiousInfo.objects.get(user=user)
if PsychologicInfo.objects.filter(user=user).exists():
psychological = PsychologicInfo.objects.get(user=user)
if InvestigationInfo.objects.filter(user=user).exists():
investigation = InvestigationInfo.objects.get(user=user)
if scientific.is_interviewed == False and religious.is_interviewed == False and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[1][0]
if scientific.is_interviewed == True and religious.is_interviewed == False and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[2][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[3][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == True and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[4][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == True and investigation.is_interviewed == True:
user.progress_level = USER_PROGRESS_LEVELS[5][0]
I think the pre_save is causing the issue because when I change it to post_save users can register but the signal wont work then. what should I do to fix this problem?
Upvotes: 0
Views: 1092
Reputation: 2425
Because you defined scientific
inside if
statement and when accessed in if scientific.is_interviewed == False
it says it is not defined.So you have to define scientific
variable before if
statement with default
value.
@receiver(pre_save, sender=User, dispatch_uid='progress_change')
def progress(sender, instance, **kwargs):
user = instance
scientific = religious = psychological = investigation = None #< ----- initialize variable
if ScientificInfo.objects.filter(user=user).exists():
scientific = ScientificInfo.objects.get(user=user)
if ReligiousInfo.objects.filter(user=user).exists():
religious = ReligiousInfo.objects.get(user=user)
if PsychologicInfo.objects.filter(user=user).exists():
psychological = PsychologicInfo.objects.get(user=user)
if InvestigationInfo.objects.filter(user=user).exists():
investigation = InvestigationInfo.objects.get(user=user)]
if scientific.is_interviewed == False and religious.is_interviewed == False and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[1][0]
if scientific.is_interviewed == True and religious.is_interviewed == False and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[2][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == False and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[3][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == True and investigation.is_interviewed == False:
user.progress_level = USER_PROGRESS_LEVELS[4][0]
if scientific.is_interviewed == True and religious.is_interviewed == True and psychological.is_interviewed == True and investigation.is_interviewed == True:
user.progress_level = USER_PROGRESS_LEVELS[5][0]
Upvotes: 2