1

I have a query object Conversation that has certain attributes.Below is the model defined for it

class Conversation(models.Model):
    timestamp = models.DateTimeField(auto_now=True)
    context_name = models.CharField(max_length=100)
    user = models.CharField(max_length=50)
    type = models.CharField(max_length=10)
    data = models.TextField(null=True)
    feedback = models.CharField(max_length=10, null=True)

Now I want to access the attributes of the queryset object.If I use the below query

serializers.serialize("json", Conversation.objects.all())

I am able to get all the attributes.But if I want the attributes based on a latest timestamp, I am unable to get it.I tried the below query

serializers.serialize("json", Conversation.objects.latest("timestamp"))

I am expecting a JSON for the latest timestamp but I get the error as TypeError: 'Conversation' object is not iterable.

However I am able to access the individual attributes like Conversation.objects.latest("timestamp").context_name but not all of them for the latest timestamp.

How can I achieve this?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
  • 1
    Possible duplicate of [How do you serialize a model instance in Django?](https://stackoverflow.com/questions/757022/how-do-you-serialize-a-model-instance-in-django) – scharette Jun 21 '18 at 16:58

0 Answers0