已解决:删除 Django 时的外键

贡献.auth

如果在 Django 中删除了 ForeignKey,那么数据库中的任何关联记录也将被删除。

-models foreign-key cascade I have a model with a ForeignKey to another model. When the referenced model is deleted, I want the ForeignKey to be set to NULL. How can I do that? Read this post in context

Django - how to get all objects from one table which are not in another table? django I have two models: class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(decimal_places=2, max_digits=10) class OrderItem(models.Model): product = models.ForeignKey('Product', on_delete=models.CASCADE) quantity = models.IntegerField() def __str__(self): return self.product How can i get all products which are not in OrderItem? Read this post in context

Django - how to create an object with a foreign key that doesn't exist yet django I am trying to create an object with a foreign key that doesn't exist yet (the user). The user will be created after the object is created and then it will be assigned as the foreign key for the object later on when it exists (in another view). This is my code: def add_to_cart(request, pk): product = get_object_or_404(Product, pk=pk) orderitem, created = OrderItem.objects ... Read this post in context

Django – 如何使用尚不存在的外键创建对象 django 我正在尝试使用尚不存在的外键(用户)创建对象。 用户将在创建对象后创建,然后当它存在时(在另一个视图中),它将被分配为对象的外键。 这是我的代码: def add_to_cart(request, pk): product = get_object_or_404(Product, pk=pk) orderitem, created = OrderItem.objects ......阅读这篇文章的上下文

Django – 如何使用尚不存在的外键创建对象 django 我正在尝试使用尚不存在的外键(用户)创建对象。 用户将在创建对象后创建,然后当它存在时(在另一个视图中),它将被分配为对象的外键。 这是我的代码: def add_to_cart(request, pk): product = get_object_or_404(Product, pk=pk) orderitem, created = OrderItem.objects ......阅读这篇文章的上下文

如何让 Django 使用我自己的自定义密码哈希算法? django 我希望 Django 使用我自己的自定义密码哈希算法而不是默认算法。 我怎样才能做到这一点? 在上下文中阅读这篇文章

什么是外键

ForeignKey 是引用另一个模型中的模型的模型字段。

on_delete 选项

在 Django 中处理删除有几个不同的选项。 最简单的选择是使用 delete() 函数:

删除(对象)

这将从数据库和任何关联数据中删除该对象。 如果该对象是模型实例,它还会使任何关联的字段无效。

另一种选择是使用 destroy() 函数:

相关文章:

发表评论