解決済み: django の削除時の ForeignKey

投稿認証

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とは

ForeignKey は、別のモデルのモデルを参照するモデル フィールドです。

on_delete オプション

Django で削除を処理するためのいくつかの異なるオプションがあります。 最も簡単なオプションは、delete() 関数を使用することです。

削除 (オブジェクト)

これにより、データベースおよび関連データからオブジェクトが削除されます。 オブジェクトがモデル インスタンスの場合、関連するフィールドも無効になります。

別のオプションは、destroy() 関数を使用することです。

関連記事:

コメント