已解決:刪除 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() 函數:

相關文章:

發表評論