Django: Cum să cascadă o actualizare prin mai multe modele?

0

Problema

Am scris un Django pe bază de cerere pentru a urmări obiecte (Objekt) și activitățile de întreținere. Objekts pot fi legate la un loc.

Locație (0/1) --- (n) Objekt (1) --- (n) Sarcină

Locație, Objekt și Sarcina toate au un statut de teren cu următoarele valori:

    RED = "red"
    YELLOW = "yellow"
    GREEN = "green"
    STATUS = [
        (RED, "Overdue tasks"),
        (YELLOW, "Pending tasks"),
        (GREEN, "All good"),
    ]

Vreau ca Locație de hartă cu marcaj își schimbă culoarea în funcție de starea legate Objekts și ultimatelly Sarcini.

Am încercat să urmeze django cele mai bune practici și de a crea un model de grăsime.

from django.db import models
from locationapp.models import Location
from taskapp.models import Task
from rules.contrib.models import RulesModel

class Objekt(RulesModel):
    RED = "red"
    YELLOW = "yellow"
    GREEN = "green"
    STATUS = [
        (RED, "Overdue tasks"),
        (YELLOW, "Pending tasks"),
        (GREEN, "All good"),
    ]
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    location = models.ForeignKey(
        Location, on_delete=models.SET_NULL, null=True, blank=True
    )
    status = models.CharField(max_length=6, choices=STATUS, default=GREEN)

    def set_status(self):
        if Task.objects.filter(objekt=self.id).filter(status=Task.RED).exists():
            self.status = Objekt.RED
        elif Task.objects.filter(objekt=self.id).filter(status=Task.YELLOW).exists():
            self.status = Objekt.YELLOW
        else:
            self.status = Objekt.GREEN

Dar cumva eu nu sunt sigur despre conceptul meu aici... Cum poate un update de pe Sarcina de a declanșa o actualizare cu privire la Objekt. Și cum s-ar Objekt continuare declanșa actualizarea pe Locație - dacă este necesar, la toate?

django model python
2021-11-22 21:49:18
1

Cel mai bun răspuns

1

O posibilă soluție este de a utiliza semnale. Am pus-o în aplicare următoarele, fără telina pentru acum:

# objektapp/apps.py
from django.apps import AppConfig


class ObjektappConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'objektapp'

    def ready(self):
        import objektapp.signals
# objektapp/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.db import transaction
from taskapp.models import Task
from objektapp.models import Objekt

@receiver(post_save, sender=Task)
def set_status(sender, instance, created, **kwargs):
    # TODO: Use celery for async operation: https://docs.djangoproject.com/en/3.2/topics/db/transactions/
    transaction.on_commit(lambda: objekt_update_status(instance))

def objekt_update_status(task_instance):
    objekt = Objekt.objects.get(id=task_instance.objekt.id)

    new_objekt_status = Objekt.GREEN
    if Task.objects.filter(objekt=task_instance.objekt.id, status=Task.RED).exists():
        new_objekt_status = Objekt.RED
    elif Task.objects.filter(objekt=task_instance.objekt.id, status=Task.YELLOW).exists():
        new_objekt_status = Objekt.YELLOW

    if objekt.status != new_objekt_status:
        objekt.status = new_objekt_status
        objekt.save()

O setare similară am făcut pe un model de Locație, care, de asemenea, reacționează la o post_save semnal de la Objekt. Nu sunt sigur daca e cel mai bun loc pentru a stoca objekt_update_status (funcția) în signals.py fișier, dar încearcă să-l pună în models.py s-a încheiat cu circulare de import de eroare.

2021-11-24 11:19:32

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................