נפתרה: ForeignKey על מחיקת django

contrib.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 – איך ליצור אובייקט עם מפתח זר שעדיין לא קיים 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():

הודעות קשורות:

השאירו תגובה