class Topic(models.Model):
Title = models.CharField(max_length=150)
UserId = models.ForeignKey(User,on_delete=models.CASCADE)
GenreId = models.ForeignKey(Genre,related_name='genres',on_delete=models.CASCADE)
StartTime = models.DateTimeField(auto_now_add=False)
Createdtime = models.DateTimeField(auto_now_add=True)
IsBlocked = models.BooleanField()
CounterLike = models.IntegerField()
IsRepoCalulated = models.IntegerField()
def __str__(self):
return self.Title
class TopicXReport(models.Model):
TopicId = models.ForeignKey(Topic,on_delete=models.CASCADE)
UserId = models.ForeignKey(User,on_delete=models.CASCADE)
Createdtime = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s: %s' % (self.TopicId, self.UserId)
class TopicXLike(models.Model):
TopicId = models.ForeignKey(Topic,on_delete=models.CASCADE)
UserId = models.ForeignKey(User,on_delete=models.CASCADE)
Createdtime = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s: %s' % (self.TopicId, self.UserId)
class TopicXVote(models.Model):
TopicId = models.ForeignKey(Topic,on_delete=models.CASCADE)
UserId = models.ForeignKey(User,on_delete=models.CASCADE)
VoteType = models.BooleanField()
status = models.BooleanField()
IsBlocked = models.BooleanField()
Createdtime = models.DateTimeField(auto_now_add=True)
def __str__(self):
if self.VoteType == True:
Votetypebool = "for it"
else:
Votetypebool = "against it"
return '%s "status" %s : %s -> %s' % (Votetypebool, self.status, self.UserId, self.TopicId)
я хотел получить api, как и все поля topi c с общим количеством лайков topicXlike и topicXreport, и я использую django подход modelsearializer фреймворка отдыха, но не смог найти spacifi c like how Могу ли я получить вывод, как я хочу, чтобы формат вывода был ниже
{
"id": 1,
"Title": "is it",
"StartTime": "2020-04-04T16:09:37.781958Z",
"Createdtime": "2020-04-14T15:31:11.459409Z",
"IsBlocked": false,
"totallike": 50, #this fields are coming from topicXlike models
"totalvotes": 50,#this fields are coming from topicXvotes models
"IsRepoCalulated": 0,
"UserId": {
"id": 2,
"username": "shilpa",
"profile": {
"Imagename": "http://127.0.0.1:8000/media/1mk1-6aYaf_Bes1E3Imhc0A.jpeg"
}
},
"GenreId": {
"id": 1,
"Name": "Current Affairs",
"Createdtime": "2020-04-14T15:31:11.343074Z"
}
}
Может кто-нибудь направить меня в правильном направлении, я немного новичок в этом спасибо