0

I have a customer model and an event model. event model refer to the customer. A customer has many events. My requirement is when I get the customer, I need the latest event object of the customer. When I call the customer Get API, it shows the following error,

TypeError at /api/v1/customer

is not JSON serializable

My Models

class Customer(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=20, unique=True)
    #some more fields to go
    def __str__(self):
        return str(self.name)


    def latest_event_name(self):
        """Return latest event """

        latest_event = self.customer_events.order_by('-event_date').last()
        return latest_event if latest_event else None



class Event(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    event_name = models.CharField(max_length=200, unique=True)

    customer = models.ForeignKey(customer_models.Customer, db_index=True, on_delete=models.SET_NULL,
                                 related_name='customer_events', null=True)
   #some more fields to go

serializer

class CustomerSerializer(serializers.ModelSerializer):
    latest_event_name = serializers.ReadOnlyField()

    class Meta:
        model = Customer
        fields = '__all__'
5
  • List the rest of the fields in Event - clearly one of them is preventing serialization. Commented Mar 7, 2017 at 7:48
  • Where are you trying to do the serialzing? If you get a CustomerEvent instance, what is it supposed to serialize into? Commented Mar 7, 2017 at 7:52
  • @shadow - Its the latest_event_name that isn't serializable since DRF has no idea how to serialize the queryset result Commented Mar 7, 2017 at 7:52
  • does latest_event_name = EventSerializer(read_only=True) do what you're looking for? (You'll need to write the event serializer) Commented Mar 7, 2017 at 7:58
  • If I use code return latest_event.event_name if latest_event else None in model,` 'latest_event_name = serializers.ReadOnlyField()' ` will work in serializer. It will return the event name. But I need the full event object. Commented Mar 7, 2017 at 9:46

2 Answers 2

2

As you are using Events in

def latest_event_name(self): ...

you'll have to create a serializer for the Event model, because that method returns an instance of the model Event.

I think that, if you just return the id of the Event as a str, you will not have that problem, so you wouldn't have to create a seralizer for the Event model.

Sign up to request clarification or add additional context in comments.

1 Comment

I need the full event object
0

Anyway now fixed the issue by the following method.

def latest_event(self):
latest_event = self.customer_events.order_by('-event_date').last()
eventDict = None
if latest_event:
    eventDict = {"event_id": latest_event.id, "event_name": latest_event.event_name}
return eventDict

Not sure whether its the correct method or not.

1 Comment

Of course you can do that, basically you are serializing the object by hand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.