无法将关键字“用户”解析为字段。选项包括:文档,ID,密码,用户名,username_id

时间:2018-06-24 08:33:49

标签: django

我的模型或尝试实现算法的方式有问题。 models.py

class Profile(models.Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE)
    password = models.TextField(max_length=80,blank=True)


class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    uploaded_by = models.ForeignKey(Profile,on_delete=models.CASCADE)
    date_uploaded = models.DateTimeField(auto_now_add=True)

上传功能(views.py)

@login_required
def upload(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'])
            profile = Profile.objects.get(user = request.user)
            newdoc.uploaded_by = profile.request.user
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('upload'))
    else:
        form = DocumentForm()  # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render(request,'upload.html',{'documents': documents, 'form': form})  

我在上传时遇到了场错误

Cannot resolve keyword 'user' into field. Choices are: document, id, password, username, username_id

它指向第57行

profile = Profile.objects.get(用户= request.user)

如果我更改此行

profile = Profile.objects.get(username = request.user)

我有这个错误

Exception Value:    
Profile matching query does not exist.

是否可以从shell获得更多信息? 到目前为止有效

In [3]: user = User.objects.get(username='drazen75')
In [6]: user.last_login
Out[6]: datetime.datetime(2018, 6, 24, 7, 1, 2, 196411, tzinfo=<UTC>)

我的数据库模式

CREATE TABLE IF NOT EXISTS "malex_document" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "docfile" varchar(100) NOT NULL, "date_uploaded" datetime NOT NULL, "uploaded_by_id" integer NOT NULL REFERENCES "malex_profile" ("id") DEFERRABLE INITIALLY DEFERRED);

编辑 Daniel建议我更改个人资料字段

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    password = models.TextField(max_length=80,blank=True)

完成,也可以迁移。 还是同样的问题。

File "/home/milenko/nup/malex/views.py" in upload
  57.             profile = Profile.objects.get(user = request.user)

File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/query.py" in get
  403.                 self.model._meta.object_name

Exception Type: DoesNotExist at /upload/
Exception Value: Profile matching query does not exist.

sqlite模式输出 .schema malex_profile

CREATE TABLE IF NOT EXISTS "malex_profile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "password" text NOT NULL, "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);

该如何解决?

1 个答案:

答案 0 :(得分:1)

由于某种原因,您调用了将配置文件链接到用户username的字段。您可以更改查询以改为使用username,但实际上您应该修复该字段。它是指用户,因此应称为user