How to change merging two queries to josnResponse - django

i'm trying to merge two queries , i did this

models.py

class MyDateTimes(models.Model):
    deadline = models.DateTimeField()

class ModelA(models.Model):
    title = models.CharField(max_length=30)
    deadline = models.OneToOneField(MyDateTimes,blank=True,null=True)
    
class ModelB(models.Model):
    post = models.ForeignKey(MyPosts,on_delete=models.PROTECT)
    deadline = models.OneToOneField(MyDateTimes,blank=True,null=True)

 class MyPosts(models.Model):
    article = models.CharField(max_length=30)

i've to merge ModelA with ModelB , here is what i did

from itertools import chain

modelA = ModelA.object.filter(deadline__isnul=False,deadline__deadline__lte=timezone.now())
modelB = ModelB.object.filter(deadline__isnul=False,deadline__deadline__lte=timezone.now())

my_query = list(chain(modelA, modelB))

my_listA = []
my_listB = []

i have to return json response of my_query

for i in modelA:
    item={
        'title':i.title,
        'dead_line':i.deadline.deadline,
    }
    my_listA.append(item)

for i in modelB:
    item={
        'title':i.post.article,
        'dead_line':i.deadline.deadline,
    }
    my_listB.append(item)

note : because i need to use ForeignKey and OneToOne data thats i couldn't use serialiser function i want to do it into one list to return JsonResponse , in order to callable into ajax call ! thank you in advance , please let me know the most efficient way to achieve that

Back to Top