From 43b0d0b4ea62d73e59892de0c57b9f9fec85602c Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Sun, 4 Jan 2026 09:09:01 +0330 Subject: [PATCH] fix - link ranchers repeatedly on every cooperative on one city --- .../commands/link_ranchers_on_org.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/apps/herd/management/commands/link_ranchers_on_org.py b/apps/herd/management/commands/link_ranchers_on_org.py index f0ace4c..b001177 100644 --- a/apps/herd/management/commands/link_ranchers_on_org.py +++ b/apps/herd/management/commands/link_ranchers_on_org.py @@ -38,30 +38,32 @@ class Command(BaseCommand): processed += 1 - # اگر قبلاً لینک داره if RancherOrganizationLink.objects.filter(rancher=rancher).exists(): skipped += 1 continue - orgs = Organization.objects.filter( - city=rancher.city, - type__key='CO' # ⚠️ کد type تعاونی خودت + orgs = list( + Organization.objects.filter( + city=rancher.city, + type__key='CO' + ) ) - if not orgs.exists(): + if not orgs: skipped += 1 continue - if orgs.count() > 1: - ambiguous += 1 - continue - - buffer.append( - RancherOrganizationLink( - rancher=rancher, - organization=orgs.first() + for org in orgs: + buffer.append( + RancherOrganizationLink( + rancher=rancher, + organization=org + ) ) - ) + + if len(orgs) > 1: + ambiguous += 1 + print(len(buffer)) if len(buffer) >= self.BATCH_SIZE: created += self.bulk_create(buffer)