Kuxazululiwe: I-ForeignKey ekususeni i-django

contrib.auth

Uma i-ForeignKey isuswa ku-Django, noma imaphi amarekhodi ahlobene kusizindalwazi azosuswa futhi.

-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

I-Django – indlela yokudala into ngokhiye wangaphandle ongekho okwamanje django Ngizama ukwakha into ngokhiye wangaphandle ongakabi khona (umsebenzisi). Umsebenzisi uzodalwa ngemva kokuthi into idaliwe bese izokwabelwa njengokhiye wangaphandle wento kamuva lapho isikhona (kokunye ukubuka). Lena ikhodi yami: def add_to_cart(request, pk): product = get_object_or_404(Umkhiqizo, pk=pk) orderitem, created = OrderItem.objects ... Funda lokhu okuthunyelwe kumongo

I-Django – indlela yokudala into ngokhiye wangaphandle ongekho okwamanje django Ngizama ukwakha into ngokhiye wangaphandle ongakabi khona (umsebenzisi). Umsebenzisi uzodalwa ngemva kokuthi into idaliwe bese izokwabelwa njengokhiye wangaphandle wento kamuva lapho isikhona (kokunye ukubuka). Lena ikhodi yami: def add_to_cart(request, pk): product = get_object_or_404(Umkhiqizo, pk=pk) orderitem, created = OrderItem.objects ... Funda lokhu okuthunyelwe kumongo

Ngingayenza kanjani i-Django isebenzise iphasiwedi yami ye-hashing algorithm? django Ngifuna i-Django isebenzise iphasiwedi yami ye-hashing algorithm esikhundleni seyokuzenzakalelayo. Ngingakwenza kanjani lokhu? Funda lokhu okuthunyelwe ngokomongo

Iyini i-ForeignKey

I-ForeignKey iyinkambu yemodeli ekhomba imodeli kwenye imodeli.

on_delete izinketho

Kunezinketho ezimbalwa ezihlukile zokuphatha ukususa ku-Django. Inketho elula ukusebenzisa umsebenzi we-delete():

susa(into)

Lokhu kuzosusa into kusizindalwazi kanye nanoma iyiphi idatha ehlobene. Uma into iyisibonelo, izophinda yenze noma yiziphi izinkambu ezihlotshaniswa zingavumelekile.

Enye inketho ukusebenzisa umsebenzi wokubhubhisa ():

Okuthunyelwe okuhlobene:

Shiya amazwana