AttributeError:Cutome用户模型对象没有属性“密码”

我有一个Profile模型,通过与User模型一对一的关系来定制User模型,该模型的代码如下

class Profile(models.Model):
    user = models.OneToOneField(User,parent_link=True,on_delete=models.CASCADE)
    bio = models.TextField()
    slug = models.SlugField(unique=True,blank=True)
    avatar_thumbnail = ProcessedImageField(upload_to='images/',default='/images/default.png',processors=[ResizeToFill(300,300)],format='JPEG',options={'quality': 60})
    location = models.TextField()
    tags = models.ManyToManyField(Tag)
    contact_information = models.TextField()
    verified = models.BooleanField(default=False)
    counter = models.IntegerField(default=0)

    def __str__(self):
        return self.user.username

    def save(self,*args,**kwargs):
        print('self.username')
        print(self.user.username)
        self.slug = self.user.username
        super(Profile,self).save(*args,**kwargs)

    def get_absolute_url(self):
        return reverse('infrastructure:edit-user-profile',kwargs={'slug': self.slug})

每当我使用以下命令进入外壳时

python manage.py shell

并获取配置文件查询集

Profile.objects.all()

我得到了错误

AttributeError: 'Profile' object has no attribute 'password'

,每当我进入管理面板并单击“个人资料”部分时 我在说同样的事情时遇到同样的错误

怎么了?

我的admin.py代码

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
# Register your models here.
from . import models

class SubjectTagAdmin(admin.TabularInline):
    model = models.SubjectTag.tags.through

class TagAdmin(admin.ModelAdmin):
    inlines=[SubjectTagAdmin,]

admin.site.register(models.Tag,TagAdmin)
admin.site.register(models.SubjectTag)
admin.site.register(models.Profile)
admin.site.register(models.Organization)

编辑

现在,只有在到达配置文件的UpdateView或从管理面板对其进行编辑时,我才会收到错误消息

从视图中查看完整的追溯:

Traceback (most recent call last):
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",line 164,in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\mixins.py",line 13,in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'user'

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\core\handlers\exception.py",line 34,in inner
    response = get_response(request)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\core\handlers\base.py",line 115,in _get_response
    response = self.process_exception_by_middleware(e,request)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\core\handlers\base.py",line 113,in _get_response
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\views\generic\base.py",line 71,in view
    return self.dispatch(request,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\views\generic\base.py",line 97,in dispatch
    return handler(request,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\views\generic\edit.py",line 189,in get
    self.object = self.get_object()
  File "C:\Users\connect\Desktop\The-Social-Work\infrastructure\views.py",line 31,in get_object
    if obj.user.username != self.request.user.username:
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",line 178,in __get__
    rel_obj = self.get_object(instance)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",line 293,in get_object
    kwargs = {field: getattr(instance,field) for field in fields}
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",in <dictcomp>
    kwargs = {field: getattr(instance,field) for field in fields}
AttributeError: 'Profile' object has no attribute 'password'

从管理面板进行完整的追溯

Traceback (most recent call last):
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",field) for field in fields}
AttributeError: 'Profile' object has no attribute 'password'
[17/Nov/2019 18:15:35] "GET /edit_usr_profile/amrshedou/ HTTP/1.1" 500 104918
[17/Nov/2019 18:20:00] "GET /admin/ HTTP/1.1" 200 5187
[17/Nov/2019 18:20:02] "GET /admin/infrastructure/profile/ HTTP/1.1" 200 4387
[17/Nov/2019 18:20:02] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
[17/Nov/2019 18:20:04] "GET /admin/infrastructure/profile/1/change/ HTTP/1.1" 200 8276
[17/Nov/2019 18:20:04] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
[17/Nov/2019 18:20:10] "POST /admin/infrastructure/profile/1/change/ HTTP/1.1" 200 8435
[17/Nov/2019 18:20:10] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
[17/Nov/2019 18:20:14] "GET /admin/infrastructure/tag/add/?_to_field=id&_popup=1 HTTP/1.1" 200 10535
[17/Nov/2019 18:20:14] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
[17/Nov/2019 18:20:15] "GET /static/admin/js/inlines.js HTTP/1.1" 200 13773
[17/Nov/2019 18:20:18] "POST /admin/infrastructure/tag/add/?_to_field=id&_popup=1 HTTP/1.1" 200 359
[17/Nov/2019 18:20:18] "GET /static/admin/js/popup_response.js HTTP/1.1" 200 569
self.username
Internal Server Error: /admin/infrastructure/profile/1/change/
Traceback (most recent call last):
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",**callback_kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\contrib\admin\options.py",line 606,in wrapper
    return self.admin_site.admin_view(view)(*args,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\utils\decorators.py",line 142,in _wrapped_view
    response = view_func(request,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\views\decorators\cache.py",line 44,in _wrapped_view_func
    response = view_func(request,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\contrib\admin\sites.py",line 223,in inner
    return view(request,**kwargs)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\contrib\admin\options.py",line 1637,in change_view
    return self.changeform_view(request,object_id,form_url,extra_context)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\utils\decorators.py",line 45,in _wrapper
    return bound_method(*args,line 1522,in changeform_view
    return self._changeform_view(request,extra_context)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\contrib\admin\options.py",line 1561,in _changeform_view
    self.save_model(request,new_object,form,not add)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\contrib\admin\options.py",line 1088,in save_model
    obj.save()
  File "C:\Users\connect\Desktop\The-Social-Work\infrastructure\models.py",line 52,in save
    print(self.user.username)
  File "C:\Users\connect\Desktop\The-Social-Work\env\lib\site-packages\django\db\models\fields\related_descriptors.py",field) for field in fields}
AttributeError: 'Profile' object has no attribute 'password'

views.py

class ProfileUpdate(UpdateView):
    model = Profile
    fields= [
        'username','bio','avatar_thumbnail','location','tags','contact_information'
        ]

    def get_object(self):
        obj = Profile.objects.get(slug = self.kwargs.get('slug'))
        if obj.user.username != self.request.user.username:
            raise exceptions.PermissionDenied()
        return obj

    def get_context_data(self,**kwargs):
        context = super(ProfileUpdate,self).get_context_data(**kwargs)
        context['tag_form'] = TagForm
        return context 

    def get_queryset(self):
        base_qs = super(ProfileUpdate,self).get_queryset()
        return base_qs.filter(username=self.request.user.username)
fkesn 回答:AttributeError:Cutome用户模型对象没有属性“密码”

我应该已经替换了一对一关系选项以自定义用户 遵循此答案

https://stackoverflow.com/a/58904426/5752406

本文链接:https://www.f2er.com/3086014.html

大家都在问