DJANGOLEARN_IR Telegram 645
جنگولرن
مشکل soft delete برای relation ها اگه این ویدئو رو از کانال @microfrontend_ir دیده باشید. تقریبا همه حالت هارو برای Soft delete هندل کرده. برای اینکه حذف واقعی اتفاق نیافته، متد delete مدل رو override می کنیم و میگیم جای حذف، رکورد رو آپدیت کن و... ولی…
کامنت @Sepehr_e
سلام و درود. من برای این یه راه حلی دیدم از ریپو یکی از دوستان بنظر اومد مربوطه
کاری که کردن اومدن و از models.CASCADE استفاده کردن و یه SOFT_CASCADE نوشتن که همه مدلای مربوطه رو سافت دلیت میکنه
from django.db import connections
from django.utils import timezone
from collections import defaultdict


def SOFT_CASCADE(collector, field, sub_objs, using):
collector.data = defaultdict(set)

collector.collect(
sub_objs,
source=field.remote_field.model,
source_attr=field.name,
nullable=field.null,
fail_on_restricted=False,
)

collector.data = defaultdict(set)

for item in sub_objs:
item.is_deleted = True
item.delete_date = timezone.now()
item.save()

if field.null and not connections[using].features.can_defer_constraint_checks:
collector.add_field_update(field, None, sub_objs)

https://github.com/mejomba/django_shop_m89_final/blob/main/core/models_on_delete.py
👍51



tgoop.com/djangolearn_ir/645
Create:
Last Update:

کامنت @Sepehr_e
سلام و درود. من برای این یه راه حلی دیدم از ریپو یکی از دوستان بنظر اومد مربوطه
کاری که کردن اومدن و از models.CASCADE استفاده کردن و یه SOFT_CASCADE نوشتن که همه مدلای مربوطه رو سافت دلیت میکنه

from django.db import connections
from django.utils import timezone
from collections import defaultdict


def SOFT_CASCADE(collector, field, sub_objs, using):
collector.data = defaultdict(set)

collector.collect(
sub_objs,
source=field.remote_field.model,
source_attr=field.name,
nullable=field.null,
fail_on_restricted=False,
)

collector.data = defaultdict(set)

for item in sub_objs:
item.is_deleted = True
item.delete_date = timezone.now()
item.save()

if field.null and not connections[using].features.can_defer_constraint_checks:
collector.add_field_update(field, None, sub_objs)

https://github.com/mejomba/django_shop_m89_final/blob/main/core/models_on_delete.py

BY جنگولرن


Share with your friend now:
tgoop.com/djangolearn_ir/645

View MORE
Open in Telegram


Telegram News

Date: |

But a Telegram statement also said: "Any requests related to political censorship or limiting human rights such as the rights to free speech or assembly are not and will not be considered." While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc. Telegram channels fall into two types: As five out of seven counts were serious, Hui sentenced Ng to six years and six months in jail. Those being doxxed include outgoing Chief Executive Carrie Lam Cheng Yuet-ngor, Chung and police assistant commissioner Joe Chan Tung, who heads police's cyber security and technology crime bureau.
from us


Telegram جنگولرن
FROM American