해결됨: 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는 다른 모델의 모델을 참조하는 모델 필드입니다.

on_delete 옵션

Django에서 삭제를 처리하기 위한 몇 가지 다른 옵션이 있습니다. 가장 간단한 옵션은 delete() 함수를 사용하는 것입니다.

삭제(객체)

이렇게 하면 데이터베이스 및 관련 데이터에서 개체가 삭제됩니다. 개체가 모델 인스턴스인 경우 연결된 모든 필드도 무효화합니다.

또 다른 옵션은 destroy() 함수를 사용하는 것입니다.

관련 게시물:

코멘트 남김