无法将关键字“用户”解析为字段。选项有:create_account、email、full_name

Posted

技术标签:

【中文标题】无法将关键字“用户”解析为字段。选项有:create_account、email、full_name【英文标题】:Cannot resolve keyword 'user' into field. Choices are: create_account, email, full_name 【发布时间】:2022-01-22 00:40:43 【问题描述】:

我创建了 2 个模型。

    帐户 用户配置文件

首先,用户注册邮箱、全名、密码1、密码2。表Account中的数据发送到数据库。

其次,用户将登录,如果成功,他将进入仪表板。在仪表板中有一个配置文件表格。 在简介表格中,他将输入数据简介,例如电话号码,出生日期。等,并将存储在表 UserProfil UserProfil 与 Account 关系中的所有数据。

我尝试创建“个人资料表格”。像这样。

我的问题是如何将数据 Full_Name 放入此表单中? 我得到错误无法将关键字“用户”解析到字段中。选项有:create_account、email、full_name ...

身份验证/models.py

class Account(AbstractBaseUser, PermissionsMixin):
      email                   = models.EmailField(_('email address'), unique=True)
      full_name               = models.CharField(max_length=150)
      create_account          = models.DateTimeField(default=timezone.now)
      is_active               = models.BooleanField(default=False)
      is_staff                = models.BooleanField(default=False)
      is_reviewer             = models.BooleanField(default=False)
      is_admin                = models.BooleanField(default=False)

      objects = CustomAccountManager()

      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['full_name']

      def __str__(self):
          return self.full_name

dashboard/models.py

class UserProfil(models.Model):
    jenis_kelamin_choice = (
       ('Pria', 'Pria'),
       ('Wanita', 'Wanita' ),
    )

    user                    = models.OneToOneField(settings.AUTH_USER_MODEL, 
                              on_delete=models.CASCADE,)
    nik                     = models.CharField(max_length=11, null=True, unique=True)
    nidn                    = models.CharField(max_length=11, null=True, unique=True)

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

dashboard/views.py

class UserProfilFormView(CreateView):
    template_name       = 'dashboard/profil.html'
    form_class          = UserProfilForm

def form_valid(self, form):
    userPofil       = form.save(commit=False)
    userPofil.user  = Account.objects.get(user__full_name=self.request.user)
    userPofil.save()
    
    messages.success(self.request, 'Data Profil Berhasil Disimpan.')
    print(self.request.user)
    return super().form_valid(form)

文件追溯:

Internal Server Error: /dashboard/profil
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "D:\Project\hibahinternal\dashboard\views.py", line 26, in 
form_valid
userPofil.user  = Account.objects.get(user__full_name=self.request.user)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\query.py", line 424, in get
clone = self._chain() if self.query.combinator else self.filter(*args, 
**kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\query.py", line 941, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\query.py", line 961, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\query.py", line 968, in 
_filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\sql\query.py", line 1393, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\sql\query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\sql\query.py", line 1286, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\sql\query.py", line 1112, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, 
self.get_meta())
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site- 
packages\django\db\models\sql\query.py", line 1539, in names_to_path
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'user' into 
field. Choices are: create_account, email, full_name, groups, id, 
is_active, is_admin, is_reviewer, is_staff, is_superuser, last_login, 
logentry, password, user_permissions, userprofil
[20/Dec/2021 19:31:48] "POST /dashboard/profil HTTP/1.1" 500 136397

谢谢

【问题讨论】:

请分享完整回溯,edit您的问题。 我只是更新回溯。感谢回复 【参考方案1】:

您的Account 没有user 字段,这也没有多大意义,因为那是用户帐户。无论如何你都不需要查询Account 模型:request.user 与用户模型一起工作,这里是帐户,所以你可以直接使用request.user

from django.contrib.auth.mixins import LoginRequiredMixin

class UserProfilFormView(LoginRequiredMixin, CreateView):
    template_name = 'dashboard/profil.html'
    form_class = UserProfilForm

    def form_valid(self, form):
        form.instance.user = request.user
        messages.success(self.request, 'Data Profil Berhasil Disimpan.')
        print(self.request.user)
        return super().form_valid(form)

注意:您可以将视图限制为基于类的视图,以向经过身份验证的用户提供 LoginRequiredMixin mixin [Django-doc].

【讨论】:

得到一个新的错误,没有要重定向到的 URL。提供一个 url 或在模型上定义一个 get_absolute_url 方法。我在哪里找到文档。我想我应该了解更多。 @您应该添加success_url,或覆盖get_success_url 以指定在创建成功时重定向到哪个URL。

以上是关于无法将关键字“用户”解析为字段。选项有:create_account、email、full_name的主要内容,如果未能解决你的问题,请参考以下文章

如何修复:无法将关键字“用户”解析为字段。选项有:description、end_time、id、start_time、title Django 错误

无法将关键字“用户”解析为字段 - django 错误

字段错误在/。无法将关键字“textField”解析为字段

无法将关键字'user'解析为字段-Django错误

无法将关键字 u'slug' 解析为 Django 中的字段错误?

Realm create() 无法将 JSON 字符串解析为 NSDate?