I am a newbie in Django. I have defined the models and a method
from django.db import models
from django.contrib.auth.models import User
class Practice(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=200)
phone = models.IntegerField()
def __str__(self):
return self.name
class Doctor(models.Model):
specialisation = models.CharField(max_length=50)
practice = models.ForeignKey(Practice, related_name='doctor',on_delete=models.DO_NOTHING)
name = models.ForeignKey(User, related_name ='doctor', on_delete=models.DO_NOTHING)
selected = models.BooleanField()
def __str__(self):
return self.specialisation
def get_list_doctors(self):
all_doctors = User.objects.exclude(pk=1).filter(doctor__isnull=False)
all_doctors_names = all_doctors.values_list('last_name', 'first_name')
return all_doctors_names
class Patient(models.Model):
name = models.ForeignKey(User, related_name='patient', on_delete=models.DO_NOTHING)
height = models.DecimalField(max_digits=6, decimal_places=2)
weight = models.DecimalField(max_digits=6, decimal_places=2)
practice = models.ForeignKey(Practice, related_name='patient',on_delete=models.DO_NOTHING)
primary_doctor = models.ForeignKey(Doctor, related_name='patient',on_delete=models.DO_NOTHING)
class Appointment(models.Model):
start_time = models.DateTimeField()
end_time = models.DateTimeField()
doctor = models.ForeignKey(Doctor, related_name='appointment',on_delete=models.DO_NOTHING)
practice = models.ForeignKey(Practice, related_name='appointment',on_delete=models.DO_NOTHING)
patient = models.ForeignKey(Patient, related_name='appointment',on_delete=models.DO_NOTHING)
This is my view
def login_user(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
messages.success(request, ('You Have Been Logged In!'))
doctor_list = get_list_doctors()
context = { 'doctor_name': doctor_list}
return render(request, 'homeunimed.html', context)
I am trying to use the method in the view. The reason for defining it in the model is so that I can reuse.
NameError at /
name 'get_list_doctors' is not defined
Request Method: POST
Request URL: http://localhost:8000/
Django Version: 2.1
Exception Type: NameError
Exception Value:
name 'get_list_doctors' is not defined
Exception Location: /Users/vinoojacob/Django/my_app/authenticate/views.py in login_user, line 27
Python Executable: /Users/vinoojacob/Django/bin/python
Python Version: 3.6.5
Python Path:
['/Users/vinoojacob/Django/my_app',
'/Users/vinoojacob/Django/lib/python36.zip',
'/Users/vinoojacob/Django/lib/python3.6',
'/Users/vinoojacob/Django/lib/python3.6/lib-dynload',
'/Users/Shared/anaconda3/lib/python3.6',
'/Users/vinoojacob/Django/lib/python3.6/site-packages']
However, I get this error. Any pointers to what is wrong. I thought you could access any methods defined in the model as long as the model is imported. Thanks for your help.
As you wrote you can access it via model. So decorate your method with @staticmethod, leave out self as argument and call it this way doctor_list = Doctor.get_list_doctors()
.
@staticmethod
def get_list_doctors():
all_doctors = User.objects.exclude(pk=1).filter(doctor__isnull=False)
all_doctors_names = all_doctors.values_list('last_name', 'first_name')
return all_doctors_names