Skip to content

Conversation

@duoi
Copy link

@duoi duoi commented Feb 9, 2018

Fix for:

TypeError at /datawizard/
int() argument must be a string, a bytes-like object or a number, not 'ContentType'

Serializer passes set of ContentType objects to ContentTypeIdField:

class RunSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    content_type_id = ContentTypeIdField(queryset=ContentType.objects.all())  <---

This object is received by to_representation with the name content_type_id, although it is the instance. pk=content_type_id fails with a TypeError as it tries to cast content_type_id to an int so it can perform the lookup.

def to_representation(self, content_type_id):
    ct = ContentType.objects.get(pk=content_type_id)  <---
    return '%s.%s' % (ct.app_label, ct.model)

Fixed by passing the PK instead:

def to_representation(self, content_type):
    ct = ContentType.objects.get(pk=content_type.pk)
    return '%s.%s' % (ct.app_label, ct.model)

@duoi duoi changed the title Fix issue #8, TypeError raised when passing ContentType instance Bugfix, TypeError raised when passing ContentType instance Feb 9, 2018
def to_representation(self, content_type_id):
ct = ContentType.objects.get(pk=content_type_id)
def to_representation(self, content_type):
ct = ContentType.objects.get(pk=content_type.pk)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, rather than redoing the lookup, we can just immediately:

def to_representation(self, content_type):
    return '%s.%s' % (content_type.app_label, content_type.model)

@duoi duoi closed this Feb 11, 2018
@sheppard
Copy link
Member

Thanks for the suggestion, this is fixed in the latest version.

@sheppard sheppard added this to the 1.1 milestone Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants