Django - Name Error when views call model's method - django

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

The function get_list_doctors(self) is pretty useless and exclude a user hardcoding its pk. This is pretty bad.
A couple of suggestions:
1) Change the line in Doctor
name = models.ForeignKey(User, related_name ='doctor', on_delete=models.DO_NOTHING)
With:
user = models. OneToOneField(User, related_name ='doctor', on_delete=models.CASCADE)
The relation is a user and if the user is gone, then you must remove the Doctor too. And also, the relation is one to one!
2) You can get the list of the doctor very easily in your 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!'))
doctors_list = Doctor.objects.exclude(user=user)
context = { 'doctors_list': doctors_list}
return render(request, 'homeunimed.html', context)
In your template, you can access the name of the doctors through the user (this is a very naive example)
{% for doctor in doctors_list %}
First name: {{ doctor.user.first_name }}
Last name: {{ doctor.user.last_name }}
{% endfor %}

Related

cant save django model object in database

I use request.POST content to save a model object after creating and authenticating a user.
the problem is that my user is created and authed but the model which extends that very users information "school_info model" in database is not saved while no error given. here is my code:
#api_view(['POST'])
#permission_classes((permissions.AllowAny, ))
def login_api(request):
post = request.POST
user = authenticate(
request,
username=request.POST['username'],
password=request.POST['password']
)
if user:
school = school_info.objects.create(
school_user=request.user,
school_name=post['school_name'],
school_manager_name=post['school_manager_name'],
school_manager_phone=post['school_manager_phone'],
class_count=post['class_count'],
students_per_class=post['students_per_class'],
daytime_importance=post['daytime_importance'],
)
school = school.save()
login(request, user)
return Response({}, status=200)
else:
return Response({}, status=401)
school_info model
class school_info(models.Model):
school_user = models.OneToOneField(User, on_delete=models.CASCADE)
school_name = models.CharField(max_length=20)
school_manager_name = models.CharField(max_length=20)
school_manager_phone = models.CharField(max_length=11)
class_count = models.IntegerField()
students_per_class = models.IntegerField()
daytime_importance = models.BooleanField(default=False)
I tried ModelForms and obj.create().save() too but both of them aren't working.
any answer would be appreciated.

NOT NULL constraint failed: users_details.user_id

I've tried to add another details form that have address and birth date on my registration form page. Every time i tried to sign up i get the NOT NULL constraint failed error.
models.py
class Details(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=60, blank=True)
birth_date = models.DateField(null=True, blank=True)
views.py
def register(request):
if request.method == 'POST':
r_form = UserRegisterForm(request.POST)
o_form = DetailsForm(request.POST)
if r_form.is_valid and o_form.is_valid():
r_form.save()
o_form.save()
username = r_form.cleaned_data.get('username')
messages.success(request, f'Your account has been created!')
return redirect('login')
else:
r_form = UserRegisterForm()
o_form = DetailsForm()
context = {
'r_form' : r_form,
'o_form' : o_form
}
return render(request, 'users/register.html', context)
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email','password1', 'password2']
class DetailsForm(forms.ModelForm):
address = forms.CharField()
birth_date = forms.DateField()
class Meta:
model = Details
fields = ['address', 'birth_date']
ERROR
While extraneous details/profile "sidecar" models are not the best practice since pluggable user models came along, this particular problem is caused by the two models not being associated.
Try
if r_form.is_valid and o_form.is_valid():
user = r_form.save()
o_form.instance.user = user
o_form.save()

Pure Django Form Tying User To Submitted Form And View

I am unsure how to tie a logged in user to a submitted form using regular Django forms. I see alot of examples using ModelForms but none (that I can tell) without using the ModelForms. In my forms.py im having a hard time figuring out how to add the author field. I cannot just add author = forms.ForeignKey or something like that. Then somehow in my view i need to call the author field to be saved into the database (my below example is my best guess and probably not right with the "tenant_form.author = request.user").
I have a model that looks like this and has a user Foreignkey setup:
class AppyModel(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
date_time_form_filled = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
I have a forms.py:
class TenantForm(forms.Form):
first_name = forms.CharField(required=False, label='First Name')
last_name = forms.CharField(required=False, label='Last Name')
I have a views.py
#login_required
def tenant_create_form_view(request):
tenant_form = TenantForm()
if request.method == 'POST':
tenant_form.author = request.user
tenant_form = TenantForm(request.POST)
if tenant_form.is_valid():
print(tenant_form.cleaned_data)
AppyModel.objects.create(**tenant_form.cleaned_data)
else:
print(tenant_form.errors)
context = {
'form': tenant_form
}
return render(request, 'fill_appy.html', context)
You should add author when the form is valid,
tenant_form = TenantForm()
if request.method == 'POST':
tenant_form = TenantForm(request.POST)
if tenant_form.is_valid():
obj = tenant_form.save(commit=False)
obj.author = request.user #add author here
obj.save()
# .. rest of code

How to hash text to md5/sha1 in django?

I want to convert text to sha1 in django. But, i'm not find the way how to do it if field attribut wrapped by the form.
This is my views:
def ubah_password_email(request, pk):
#cek session
if 'username' in request.session and request.session['hak_akses'] == 'user':
user = get_object_or_404(User, pk=pk) #ambil id dengan get
profile = UserProfile.objects.filter(user=user).first()
email_form = EmailForm(data=request.POST, instance=profile) #gunakan instance untuk mengambil data yang sudah ada
users = User.objects.all()
if request.POST:
if email_form.is_valid():
email = email_form.save(commit=False)
email.save()
return redirect('home')
else:
email_form = EmailForm(instance=profile)
data = {
'email_form': email_form,
'object_list': users,
}
return render(request, 'ubah_password_email.html', data)
else:
return HttpResponseRedirect('/simofa/logout')
This is my model
class UserProfile(models.Model):
user = models.OneToOneField(User) #digunakan untuk relasi ke model User (default) alias UserProfile adalah sebagai extending model
CATEGORY_CHOICES = (
('admin','Admin'),
('user','User'),
)
hak_akses = models.CharField(max_length=100, choices = CATEGORY_CHOICES)
password_email = models.CharField(max_length=100, blank=True)
password_pckelas = models.CharField(max_length=100, blank=True)
# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username
This is my forms
class EmailForm(forms.ModelForm):
password_email = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = UserProfile
fields = ('password_email',)
i'm trying using this and this reference. But, i still can't convert text to sha1?
I'm very grateful for your input. So, please help me :)
I'm not sure why you want or need a second password field. But make_password allows you to generate a hashed password:
from django.contrib.auth.hashers import make_password
Docs: https://docs.djangoproject.com/en/1.7/topics/auth/passwords/#django.contrib.auth.hashers.make_password
Source: https://github.com/django/django/blob/master/django/contrib/auth/hashers.py#L58

django logged in userprofile and relating additional form

I have question regarding how to attach additional form to logged in users in Django.
I want that additional form belongs to logged in user and the data I enter in the form should goes under logged in user table. I am new to Django and python please have patience I hope i can explain correctly what i want to do with this
Data I shall enter for this view shall go under logged in user only basically i want to attach this view to the logged in user only Error I am getting is
Exception Value:
registration_todos.user_id may not be NULL
#models
class userProfile(models.Model):
user = models.OneToOneField(User)
birth =models.DateField()
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class todos(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
created = models.DateField()
time = models.TimeField()
def __unicode__(self):
return unicode(self.user)
#forms additional form for todos
class formtodos(ModelForm):
title = forms.CharField(label=(u'Todo'))
created = forms.DateField(label=(u'Date'))
time = forms.TimeField(label=(u'Time'))
#user = forms.CharField(label=(u'username'))
class Meta:
model = todos
exclude=('user',)
#view
def modeltodo(request):
if request.user.is_authenticated():
todos.objects.filter(user=request.user)
if request.method == 'POST':
form =formtodos(request.POST)
if form.is_valid():# All validation rules pass
todoss = form.save(commit=False)
todoss.created_by = request.user
form.save()
return HttpResponseRedirect('/profile/')
else:
form = formtodos() # An unbound form
context = {'form':form}
return render_to_response('todo.html', context, context_instance=RequestContext(request))
you've specified exclude = ('user',) in your form. This means that when you try to save the form there is no user_id present which causes the error. You probably want to put this before the save() call: todoss.user = request.user