wq / django-data-wizard

🧙⚙️ Import structured data (e.g. Excel, CSV, XML, JSON) into one or more Django models via an interactive web-based wizard
https://django-data-wizard.wq.io
MIT License
341 stars 53 forks source link

Bugfix, TypeError raised when passing ContentType instance #9

Closed duoi closed 6 years ago

duoi commented 6 years ago

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)
sheppard commented 5 years ago

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