แก้ไขแล้ว: ForeignKey ในการลบ django

สนับสนุน.auth

หาก ForeignKey ถูกลบใน Django ระเบียนที่เกี่ยวข้องในฐานข้อมูลจะถูกลบด้วย

-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 – วิธีสร้างวัตถุด้วย Foreign Key ที่ยังไม่มี django ฉันกำลังพยายามสร้างวัตถุด้วย Foreign Key ที่ยังไม่มี (ผู้ใช้) ผู้ใช้จะถูกสร้างขึ้นหลังจากสร้างอ็อบเจ็กต์แล้ว จากนั้นผู้ใช้จะถูกกำหนดให้เป็นคีย์นอกสำหรับออบเจ็กต์ในภายหลังเมื่อมีอยู่ (ในมุมมองอื่น) นี่คือรหัสของฉัน: def add_to_cart(request, pk): product = get_object_or_404(Product, pk=pk) orderitem, created = OrderItem.objects … อ่านโพสต์นี้ในบริบท

Django – วิธีสร้างวัตถุด้วย Foreign Key ที่ยังไม่มี django ฉันกำลังพยายามสร้างวัตถุด้วย Foreign Key ที่ยังไม่มี (ผู้ใช้) ผู้ใช้จะถูกสร้างขึ้นหลังจากสร้างอ็อบเจ็กต์แล้ว จากนั้นผู้ใช้จะถูกกำหนดให้เป็นคีย์นอกสำหรับออบเจ็กต์ในภายหลังเมื่อมีอยู่ (ในมุมมองอื่น) นี่คือรหัสของฉัน: 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() :

กระทู้ที่เกี่ยวข้อง:

แสดงความคิดเห็น