Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrading from 3.3.0 to 3.3.1 causes error: FieldDoesNotExist: <Model> has no field named u'id' #3674

Closed
beaugunderson opened this issue Nov 25, 2015 · 7 comments · Fixed by #4571
Labels
Milestone

Comments

@beaugunderson
Copy link

I believe it may be related to #3593.

See here for a model that causes this error when serialized.

I think the issue affects models that have a primary key that's not called id. (In this model value is the primary key).

@xordoquy
Copy link
Collaborator

Not directly linked though similar. #3593 was for related field while you have the issue on the main model.

@beaugunderson
Copy link
Author

Here's the smallest possible test case, it works in 3.3.0 and fails in 3.3.1:

from django.conf import settings

if not settings.configured:
    settings.configure(
        MIGRATION_MODULES={'__main__': 'migrations'},
        INSTALLED_APPS=('__main__',))

from django.apps import apps

apps.populate(settings.INSTALLED_APPS)

from django.db import models
from rest_framework import serializers


class TestParentModel(models.Model):
    title = models.CharField(max_length=64)


class TestChildModel(models.Model):
    parent = models.ForeignKey(TestParentModel, related_name='children')
    value = models.CharField(primary_key=True, max_length=64)


class TestChildModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = TestChildModel
        fields = ('value',)


class TestParentModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = TestParentModel
        fields = ('id', 'title', 'children')


parent = TestParentModel(title='abc')
child = TestChildModel(value='def', parent=parent)

serializer = TestParentModelSerializer(parent)

print serializer.data

@benred42
Copy link

benred42 commented Dec 2, 2015

So, I did a little digging around on this issue using the test case posted above and here's what I found:

The issue does appear to be coming from the change made in #3593 in serializers.py. It looks like what is happening is when relation_info.related_model._meta.get_field(to_field).primary_key gets run in build_relational_field(), the to_field it is using is actually the pk for the TestParentModel, not the child. This happens because when _get_reverse_relationships() in model_meta.py tries to resolve the to_field it uses the field from the ForeignKeyObject for the forward relationship, not the related object, so it ends up using its own pk instead of TestChildModel's.

Sorry if that is really confusing. Tracing all those relationships has left me somewhat incoherent. I'll put up a PR with a fix that solves the issue and maybe that will make things a bit clearer, although I'm not convinced it is the best/right fix.

@beaugunderson
Copy link
Author

Thanks for digging into this @benred42! Happy to test your PR in my environment when it's ready. :)

@beaugunderson
Copy link
Author

beaugunderson commented Dec 2, 2015

Noting that #3696 does fix the errors in my environment.


Note from @tomchristie this is now #3852

@tomchristie
Copy link
Member

@beaugunderson Okay, good to know. If we can replicate in a test case then we're good to go!

@xordoquy xordoquy modified the milestones: 3.3.2 Release, 3.3.3 Release Dec 14, 2015
@xordoquy xordoquy modified the milestones: 3.3.3 Release, 3.3.4 Release, 3.4.0 Release Feb 11, 2016
@beaugunderson
Copy link
Author

It looks like this has a good PR that includes a good test case now: #3852. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants