- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当删除发挥作用时,我对 OneToOneField 如何工作有点困惑。我能找到的唯一准权威信息来自this thread on django-developers :
I don't know if you discovered this yet, but the delete is working in one direction, but not in the direction you're expecting it to. For instance, using the models you posted in another message:
class Place(models.Model):
name = models.CharField(max_length = 100)
class Restaurant(models.Model):
place = models.OneToOneField(Place, primary_key=True)If you create a Place and a Restaurant that is linked to it, deleting the Restaurant will not delete the Place (this is the problem you're reporting here), but deleting the Place will delete the Restaurant.
我有以下模型:
class Person(models.Model):
name = models.CharField(max_length=50)
# ... etc ...
user = models.OneToOneField(User, related_name="person", null=True, blank=True)
通过这种方式设置,我可以使用 user.person
从 User
实例轻松访问 person
。
但是,当我尝试删除管理中的 User
记录时,它自然会向后级联到我的 Person
模型,正如线程所讨论的那样,显示了一些内容的:
您确定要删除用户“JordanReiter2”吗?以下所有相关项目都将被删除:
不用说,我不想想要删除Person
记录或其任何后代!
我想我理解这个逻辑:因为 Person 记录中的 OneToOne 字段中有一个值,删除 User 记录会在数据库的 user_id
列中创建错误引用。
通常,解决方案是切换 OneToOne
字段所在的位置。当然,这实际上是不可能的,因为 User
对象几乎是由 django.contrib.auth
设置的。
有什么方法可以防止删除级联,同时仍然可以通过用户
直接访问人员?创建扩展 django.contrib
版本的 User
模型是唯一方法吗?
我更改了模型名称,希望现在更清晰一些。基本上,有数千条人员记录。并非每个人都有登录名,但即使有,他们也只有一个登录名。
最佳答案
结果是ForeignKey
and OneToOneField
have an attribute on_delete
您可以将其设置为 models.SET_NULL
,如下所示:
class Person(models.Model):
name = models.CharField(max_length=50)
# ... etc ...
user = models.OneToOneField(User, on_delete=models.SET_NULL, related_name="person", null=True, blank=True)
这导致了我想要的行为:User
模型被删除,而不触及 Person
记录。我忽略了它,因为它没有明确列在 OneToOneField
下,它只是说
Additionally, OneToOneField accepts all of the extra arguments accepted by ForeignKey...
很容易错过。
关于django - 与用户模型(django.contrib.auth)的一对一关系,无需级联删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6963252/
我只想在我保存的单个文件上运行coffee lint和coffee compile。我的项目中有成百上千个CoffeeScript文件,而编译它们都需要太多时间。 这是我的Gruntfile: mod
我正在使用 grunt-contrib-compass 来处理我的 .SCSS 文件 compass: { dist: { options: { cssDir: 'www/st
不幸的是,grunt-contrib-watch和 grunt-contrib-connect看起来不太好。 关于 grunt-contrib-connect readme它说: Note that
我是 nodeJS 和 grunt 的新手。我在这个项目中有这个 Gruntfile,我想对我项目中的所有 html 文件进行实时重新加载,这样我就不必一直刷新浏览器来检测新的更改。不知何故,我遇到了
我已经根据这些下载并构建了open cv open cv docs . 我正在尝试编译这个 Eigenfaces demo ,我收到以下错误。 fatal error: 'opencv2/contri
我有两个目录 src 和 compiled。我想使用 Grunt Watch 确保从 src 到 compiled 的单向数据同步.作为中间步骤,我想编译 *.less 文件以及使用 ES6 语法编写
我创建了一个 lein 项目, 比使用 eclips 的 dev-dependency 更新它的 project.clj 我跑了“lein deps” 它下载了所有依赖项 但是在 my.m2/repo
我有一些在 RStudio 中完美执行的 R 代码,但是当我在 cmd 中运行它时,我收到以下错误。 Installing package into 'C:/Users/Anish/Documents
我正在尝试为上下文强盗问题 (https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part
序曲: Ant-Contrib 为安装和启动提供了一个很好解释的 wiki 页面 ([ http://ant-contrib.sourceforge.net/#install][1] )。 事实上,只
当我尝试使用 Ant-contrib 的 FOR 循环时遇到以下错误: Invalid type class org.apache.tools.ant.taskdefs.optional.script
我正在使用DapperExtensions v4.0.30319,并且试图让Dapper.Contrib知道我的架构不是DBO。我提供了: public class EngineMapper : Cl
他们无论如何要直接生成缩小的 js 吗?我不想使用其他命令,例如 grunt uglify 因为源映射不会指向咖啡而是指向 js 最佳答案 我明白你对源 map 的看法,但你仍然可以在一个链中使用 u
我是整个 JVM 方面的新手,正在尝试使用 clojure。我正在尝试加载 clojure-contrib 但失败了: # in bash $ java -cp /path/to/clojure.ja
我正在使用 ant,并且在 if/then/else 任务(ant-contrib-1.0b3.jar)上遇到问题。 我正在运行一些可以使用下面的 build.xml 进行简化的东西。 我期待从 'a
我正在使用grunt-contrib-jasmine运行我的JavaScript规范。运行规范时如何将调试输出写入控制台,即如何获取 console.log("something"); 在控制台中显示
占用的问题 我运行以下命令来安装旧版本的opencv-contrib-python(4.0.0.21)。然后发生错误,无法安装此软件包。如何解决此错误? pip install opencv-cont
互联网上是否有可用的 Wicket Contrib Examples 的源代码- CD应用程序?谢谢。 最佳答案 您说您在对 Don 的回答的评论中查看了“svn”;哪个SVN?是这个吗? http:
我在 Maven 中使用 FindBugs,使用以下配置: org.codehaus.mojo findbugs-mave
我的构建脚本中有以下任务。 我从我的 Mac 机器运行这个构建脚本。它给出了以下错误。 Problem: failed to cr
我是一名优秀的程序员,十分优秀!