NameError:

14 views
Skip to first unread message

tech george

unread,
Apr 8, 2022, 1:50:50 PM4/8/22
to django...@googlegroups.com
Hello,

I've been struggling with the below error, what might be the problem?

I have used the same ForeignKey in other models.. all works ok, except this.

Please advise.

Models:

class CustomUser(AbstractUser):
    user_type_data = ((1, "AdminHOD"), (2, "Pharmacist"), (3, "Doctor"), (4, "PharmacyClerk"),(5, "Patients"))
    user_type = models.CharField(default=1, choices=user_type_data, max_length=10)



class Patients(models.Model):
    gender_category=(
        ('Male','Male'),
        ('Female','Female'),
    )

    nurse = models.ForeignKey(Pharmacist, related_name='Pharmacist', on_delete=models.CASCADE, null=True)
    admin = models.OneToOneField(CustomUser,null=True, on_delete = models.CASCADE)
    reg_no=models.CharField(max_length=30,null=True,blank=True,unique=True)
    gender=models.CharField(max_length=7,null=True,blank=True,choices=gender_category)
    first_name=models.CharField(max_length=20,null=True,blank=True)
    last_name=models.CharField(max_length=20,null=True,blank=True)
    date_admitted=models.DateTimeField(auto_now_add=True, auto_now=False)
    last_updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return str(self.admin)


Error:

nurse = models.ForeignKey(Pharmacist, related_name='Pharmacist', on_delete=models.CASCADE, null=True)
NameError: name 'Pharmacist' is not defined


Regards,




Antonis Christofides

unread,
Apr 8, 2022, 4:11:11 PM4/8/22
to django...@googlegroups.com

Hi! In the line where the error occurs, Pharmacist has not been defined yet. Move your Pharmacist model before your Patients model.

Regards,

Antonis

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYG20GbMnoGjkT7VtD1FFMw3Tr8Axaepnf27vDyvqYvMb%2BUiQ%40mail.gmail.com.

tech george

unread,
Apr 9, 2022, 3:38:22 AM4/9/22
to django...@googlegroups.com
Hello Antonis,

Thanks, it worked.

Much appreciated.

Regards,

Reply all
Reply to author
Forward
0 new messages