扩展用户模型时的属性错误

时间:2017-10-25 19:51:14

标签: django django-models

我正在使用Django 1.11和Python 3.目前我的LDAP身份验证正在运行。但是,我想扩展我的用户模型,并且在尝试扩展我的用户模型时遇到以下错误。

AttributeError: 'User' object has no attribute 'profile'
[25/Oct/2017 14:47:28] "POST /account/login/ HTTP/1.1" 500 224535

我试图将我的代码更改为user.profile或user.AllEeActive但是它给了我以下错误:

  File "C:\python\security\accounts\models.py", line 52, in save_user_profile
    instance.username.save()
AttributeError: 'str' object has no attribute 'save'
[25/Oct/2017 14:43:31] "POST /account/login/ HTTP/1.1" 500 224521

以下是我的models.py,我做错了什么?

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from django.forms import ModelForm
from django.db.models.signals import post_save
from django.dispatch import receiver
import django_filters




class AllEeActive(models.Model):

    employee_ntname = models.OneToOneField(User, db_column='Employee_NTName',max_length=12)  # Field name made lowercase.
    employee_last_name = models.CharField(db_column='Employee_Last_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    employee_first_name = models.CharField(db_column='Employee_First_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    b_level = models.CharField(db_column='B_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    group_name = models.CharField(db_column='Group_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    r_level = models.CharField(db_column='R_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    division_name = models.CharField(db_column='Division_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    d_level = models.CharField(db_column='D_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    market_name = models.CharField(db_column='Market_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    coid = models.CharField(db_column='COID', max_length=50, blank=True, null=True)  # Field name made lowercase.
    unit_no = models.CharField(db_column='Unit_No', max_length=50, blank=True, null=True)  # Field name made lowercase.
    dept_no = models.CharField(db_column='Dept_No', max_length=50, blank=True, null=True)  # Field name made lowercase.
    department_desc = models.CharField(db_column='Department_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    employee_status = models.CharField(db_column='Employee_Status', max_length=50, blank=True, null=True)  # Field name made lowercase.
    job_desc = models.CharField(db_column='Job_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    position_desc = models.CharField(db_column='Position_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_last_name = models.CharField(db_column='Supervisor_Last_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_first_name = models.CharField(db_column='Supervisor_First_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_job_desc = models.CharField(db_column='Supervisor_Job_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    cfo = models.CharField(db_column='CFO', max_length=255, blank=True, null=True)  # Field name made lowercase.
    email_address = models.CharField(db_column='Email_Address', max_length=250, blank=True, null=True)  # Field name made lowercase.
    location_code = models.CharField(db_column='Location_Code', max_length=20, blank=True, null=True)  # Field name made lowercase.
    location_code_desc = models.CharField(db_column='Location_Code_Desc', max_length=255, blank=True, null=True)  # Field name made lowercase.
    corporate_flag = models.CharField(db_column='Corporate_Flag', max_length=1, blank=True, null=True)  # Field name made lowercase.
    hire_date = models.DateTimeField(db_column='Hire_Date', blank=True, null=True)  # Field name made lowercase.
    termination_date = models.DateTimeField(db_column='Termination_Date', blank=True, null=True)  # Field name made lowercase.
    employee_status_id = models.IntegerField(db_column='Employee_Status_ID', blank=True, null=True)  # Field name made lowercase.
    qv_statusid = models.IntegerField(db_column='QV_StatusID', blank=True, null=True)  # Field name made lowercase.
    lawson_status_id = models.IntegerField(db_column='Lawson_Status_ID', blank=True, null=True)  # Field name made lowercase.
    load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True)  # Field name made lowercase.

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        AllEeActive.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()


    class Meta:
        managed = False


    def __str__(self):
        return self.employee_ntname

1 个答案:

答案 0 :(得分:0)

用户没有profile属性,因此请更改:

instance.profile.save()

为:

AllEeActive.objects.get(user = instance).save()