tomchristie/django-rest-framework — Issue #180: Wrong serializer used when a resource is specified as a string
Author: izquierdo
Created: 2012-03-01 06:50:42+00:00
State: open

# Background:

I have a resource `A` that has as one of its fields a nested resource `B` specified in the `(“fieldName”, resource)` format where `resource` is the name of a ModelResource reference as a string.

The nested ModelResource `B` reference has its own `fields` specification.

I'm using Django's runserver (i.e. this is my dev machine, I haven't tried what happens in a production server)

# Problem:

What happens is that the first time I GET resource `A` after restarting the server, I get *all* of the fields of resource `B`'s model. All reloads after that will only show the fields defined in its `fields` specification which is the expected behavior.

# Cause:

Resource `B` is not registered the first time, because the module it's defined in hasn't been imported yet (so `_RegisterSerializer`'s code hasn't run for that class and it's not in `_serializers`). In my case, a call to `reverse()` further down the line causes the module to be imported, so further GETs show the fields correctly.

I don't have much time to look into this now. I'll get to it later. Or if anyone else is having the same problem and wants to have a go at fixing it that would be great.