我正在制作休假申请模块,我得到了#34;用户没有属性"错误
models.py
from django.db import models
from django.contrib.auth.models import User;
from django.db.models.signals import post_save
from django.dispatch import receiver
LEAVE_CHIOCE = (
('casual', 'Casual Leave'),
('vacation', 'Vacation Leave'),
('commuted', 'Commuted Leave'),
('special_casual', 'Special Casual Leave'),
('restricted', 'Restricted Leave'),
('st
ation', 'Station Leave'),
)
APPLICATION_STATUSES = (
('accepted', 'Accepted'),
('rejected', 'Rejected'),
('processing', 'Being Processed')
)
class Leave(models.Model):
user = models.OneToOneField(User,related_name='applied_for', on_delete=models.CASCADE);
leave_type = models.CharField(max_length=20, choices=LEAVE_CHIOCE);
applied_time = models.DateTimeField(auto_now = True);
start_date = models.DateField();
end_date = models.DateField();
purpose = models.CharField(max_length=500, blank=True);
leave_address = models.CharField(max_length=100, blank=True);
processing_status = models.CharField(max_length=20, choices=APPLICATION_STATUSES);
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Leave.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.leave.save()
forms.py
from django import forms;
from . models import Leave, ApplicationRequest, RemainingLeaves;
from django.contrib.auth.models import User;
from django.contrib.auth.forms import UserCreationForm;
from datetime import datetime
class UserForm(forms.ModelForm):
class Meta:
model = User;
fields = ('username', 'first_name', 'last_name', 'email');
class LeaveForm(forms.ModelForm):
model = Leave;
class Meta:
fields = ( 'leave_type', 'start_date', 'end_date', 'purpose', 'leave_address');
view.py
from __future__ import unicode_literals
from django.contrib.auth.models import PermissionsMixin
from django.shortcuts import render
from django.http import HttpResponse;
from . models import Leave, ApplicationRequest, RemainingLeaves;
from django.contrib.auth.decorators import login_required;
from . forms import LeaveForm, UserForm;
from django.db import transaction
from django.contrib.auth.models import User
import os;
# Create your views here.
@login_required(login_url='/login')
@transaction.atomic
def getLeaveTemplate(request):
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)));
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user);
leave_form = LeaveFor[enter image description here][1]m(request.POST, instance=request.user.leave)
if user_form.is_valid() and leave_form.is_valid():
user_form.save();
leave_form.save();
return HttpResponse("Thanksss");
else:
user_form = UserForm(instance=request.user);
leave_form = LeaveForm(instance=request.user.leave);
return render(request, BASE_DIR+"/templates/pages/leaveform.html", {'user_form':user_form});
答案 0 :(得分:1)
用于从相关对象到此对象的关系的名称。
您在以下行声明related_name
为applied_for
:
user = models.OneToOneField(User, related_name='applied_for', on_delete=models.CASCADE)
这意味着Leave
对象将具有用于获取相关user
对象的属性User
。要从User
对象转到相关的Leave
对象,系统会添加属性[related_name]
。这是你的applied_for
。要删除您的错误,只需将每个request.user.leave
更改为request.user.applied_for
。
要将applied_for
属性更改为leave
,您可以将定义更改为以下内容:
user = models.OneToOneField(User, related_name='leave', on_delete=models.CASCADE)
但related_name
是一个可选参数,它有一个有用的default value:
如果没有为OneToOneField指定related_name参数,Django将使用当前模型的小写名称作为默认值。
因此,在您的情况下,不提供默认值会将属性leave
添加到User
个对象
# in model
user = models.OneToOneField(User, on_delete=models.CASCADE)
# in view
request.user.leave