Django从dict获取键,值并将它们添加到查询中

时间:2017-08-01 15:40:47

标签: django django-models

我想知道是否有一个很好的方法来实现这个目标:

class Notification(models.Model):
    target_clients_query = JSONField(blank=True)

我有一个带有JSONField的模型(我可以更改它)。它保留了将接收通知的Client对象的查询。

class Client(models.Model):
    user = models.OneToOneField(User, null=False, related_name='client_profile', db_index=True)
    phone = models.CharField(max_length=15, db_index=True, blank=True)
    cart = models.ForeignKey(Cart, null=True, blank=True, related_name='client', db_index=True)
    profile_picture = models.ImageField(null=True, blank=True, upload_to=get_uuid_client_image)
    facebook_user_id = models.CharField(blank=True, max_length=250)
    email_order_notifications = models.BooleanField(default=True)
    current_sector = models.ForeignKey("Sector", null=True)

我想将查询数据保存在JSONField中,然后,当我处理Notification对象时,让相应的客户端发送通知给:

from datetime import datetime
from django.core.management.base import BaseCommand
from app.misuper.models import Notification

class Command(BaseCommand):
    """
    Sends notifications to users
    """
    def handle(self, *args, **options):

        actual_time = datetime.today()
        notifications_to_send = Notification.objects.filter(sent=False, programmed_date__lte=actual_time.today())

        for notification in notifications_to_send:
            if notification.target_client:
                notification.send_notification()
            else:
                clients_query = notification.target_clients_query
                for key, value in clients_query:
                    pass
                    # Query the Client's model with the filters in the clients query dict

0 个答案:

没有答案
相关问题