[英]In Django 1.8.2, how can I assign a “Task” model to a user account and let the user delete their own tasks?
I want to add a user account system to a website where the user simply adds tasks he or she needs to do, with the option to check mark each one and delete all existing checkmarks in their account. 我想在网站上添加一个用户帐户系统,用户可以在该网站上简单地添加他或她需要执行的任务,并可以选中每个复选框并删除其帐户中所有现有的选中标记。
So far, I was able to create a rough website where people could go in, add tasks, and modify them, with the help of jQuery and a few functions I've defined in my views module. 到目前为止,借助jQuery和我在views模块中定义的一些功能,我已经能够创建一个粗糙的网站,人们可以进入,添加任务和对其进行修改。 I also made sure, with the help of a partner of mine, that I only do requests and responses for data only instead of the entire HTML page, as well as having my JavaScript run somewhat based on the backend where all of my tasks are saved. 我还确保在我的一个合作伙伴的帮助下,我只对数据而不是整个HTML页面进行数据的请求和响应,并确保我的JavaScript在某种程度上基于保存所有任务的后端运行。
I need to learn how the Django framework works. 我需要学习Django框架的工作方式。
A user can have multiple tasks in their checklist, so I must define a ForeignKey field in my ToDoChecklistTask model class. 用户的清单中可以有多个任务,因此我必须在ToDoChecklistTaskTask模型类中定义ForeignKey字段。 According to Chapter 14 of the Django Book, though, I'm using Django's default user model from the django.contrib.auth module, so I don't know how to connect each ForeignKey to the user that is logged in currently to the site. 但是,根据Django书的第14章 ,我使用的是django.contrib.auth模块中的Django默认用户模型,因此我不知道如何将每个ForeignKey与当前登录到该站点的用户连接。 。 I also don't know how to handle the request where the user wants to delete all the tasks that belong to him or her; 我也不知道如何处理用户要删除属于他或她的所有任务的请求; so far, I only am telling Django to remove all the tasks from the server. 到目前为止,我只告诉Django从服务器中删除所有任务。
How can I connect each user to their individual tasks based on ForeignKey? 如何根据外键将每个用户连接到他们各自的任务? And how can I access their tasks to delete them when the user sends the request to do so? 当用户发送请求时,如何访问他们的任务以将其删除?
EDIT: I think a better question to ask is, what's the name of the class I must pass in the ForeignKey constructor that represents the individual user? 编辑:我想问一个更好的问题是,我必须在代表单个用户的ForeignKey构造函数中传递的类的名称是什么?
There are a couple of points here. 这里有几点。
If you need to extend the user model, use a onetoone on User . 如果需要扩展用户模型, 请在User上使用单音 。
If you need to know the user behind the request, use request.user and 如果您需要了解请求背后的用户,请使用request.user和
from django.contrib.auth.models import User
class myModel(models.Model)
user = models.OneToOneField(User)
This would let you do things like request.user.task_set.all().delete()
这会让您执行诸如request.user.task_set.all().delete()
If you want to do all of this over AJAX, the world gets more complicated. 如果您想通过AJAX进行所有这些操作,世界将变得更加复杂。 Permissions over REST APIs are, as I understand, a semi-unsolved problem. 据我了解,REST API的权限是一个尚未解决的问题。 See this PyCon talk . 请参阅此PyCon演讲 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.