int()参数必须是字符串或数字,而不是'文件'

时间:2013-11-27 16:47:34

标签: django django-rest-framework

使用模型的DjangoREST页面上传文件时出现奇怪的错误。这是下面的模型

class Documents(models.Model):
    id = models.AutoField(primary_key=True)
    file = models.ForeignKey(Files, related_name="files")
    divider_name = models.TextField(null=True, verbose_name="Divider", blank=True)
    separator = models.TextField(null=True, verbose_name="Separator", blank=True)
    date_changed = models.DateTimeField(auto_now_add=True, verbose_name="Date Changed", blank=True)
    date_document = models.DateTimeField(auto_now_add=True, verbose_name="Document Date", blank=True)
    date_created = models.DateTimeField(auto_now_add=True, auto_now=True, verbose_name="Date Created", blank=True)
    date_filed = models.DateTimeField(auto_now_add=True, auto_now=True, verbose_name="Date Uploaded", blank=True)
    last_viewed = models.DateTimeField(auto_now_add=True, auto_now=True, verbose_name="Last Viewed", blank=True)
    status = models.IntegerField(default=1, verbose_name="Deleted", blank=True)
    sort_order = models.IntegerField(default=1, blank=True)
    description = models.TextField(verbose_name="Document Details", blank=True)
    user_filed = models.IntegerField(verbose_name="Filed By", blank=True)
    user_created = models.IntegerField(verbose_name="Created By", blank=True)
    document_locked = models.IntegerField()
    document_secured = models.IntegerField()
    document_encrypted = models.IntegerField()
    archive_documentid = models.IntegerField()
    extension = models.TextField()
    pages = models.IntegerField()
    file_size = models.IntegerField()
    full_text = models.TextField(null=True, blank=True)
    document_cleaned = models.IntegerField(null=True, blank=True)
    bacth_date = models.DateTimeField(auto_now_add=False, null=True, blank=True)
    document_contents = models.TextField(null=True, blank=True)
    process_stepid = models.IntegerField(null=True, blank=True)
    rev_document_id = models.IntegerField(null=True, blank=True)
    document_location = models.FileField(upload_to=settings.MEDIA_ROOT)

    def __unicode__(self):
        return self.id

    class Meta:
        permissions = (('can_view_document', 'Can View Document'),
                       ('can_not_view_document', 'Can Not View Document'),
                       ('can_delete_document', 'Can Delete Document'),
                       ('can_edit_document', 'Can Edit Document'),
                       ('can_export_document', 'Can Export Document'),
                       ('can_move_document', 'Can Move Document'),
                       ('can_add_document', 'Can Add Document'),)
        verbose_name = "Document"
        verbose_name_plural = "Documents"

这是序列化程序

class DocumentSerializer(serializers.ModelSerializer):
    file_id = serializers.PrimaryKeyRelatedField(queryset=Files.objects.all(), many=False)
    class Meta:
        model = Documents
        fields = ('id','file_id', 'divider_name', 'separator', 'date_changed', 'date_document', 'date_created'
                  , 'date_filed', 'last_viewed', 'status', 'sort_order', 'description', 'user_filed', 'user_created'
                  , 'document_locked', 'document_secured', 'document_encrypted', 'archive_documentid', 'extension'
                  , 'pages', 'file_size', 'eform_id', 'full_text', 'document_cleaned', 'bacth_date', 'document_contents'
                  , 'eform_due', 'efrom_complete', 'process_stepid', 'rev_document_id', 'document_location')

这是视图

class DocumentsViewSet(generics.ListCreateAPIView):
    """
    API endpoint that allows documentss to be viewed or edited.
    """
    queryset = Documents.objects.all()
    serializer_class = DocumentSerializer

因此,当我点击提交附带的PNG文件时,它会给我错误。

Request Method: POST
Request URL:    http://localhost:8080/documents/
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
int() argument must be a string or a number, not 'Files'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 613
Python Executable:  /usr/bin/python
Python Version: 2.7.3

错误讯息:

Environment:


Request Method: POST
Request URL: http://localhost:8080/documents/

Django Version: 1.6
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'south',
 'rest_framework',
 'guardian',
 'avatar',
 'django_tables2',
 'serializers',
 'templatetag_handlebars',,
 'main',
 'administration',
 'api')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  399.             response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  396.             response = handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/generics.py" in post
  456.         return self.create(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/mixins.py" in create
  52.             self.object = serializer.save(force_insert=True)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py" in save
  560.             self.save_object(self.object, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py" in save_object
  935.         obj.save(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
  545.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
  654.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _do_insert
  687.                                using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
  232.         return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
  1511.     return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  897.         for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in as_sql
  855.                 for obj in self.query.objs
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py" in get_db_prep_save
  1224.                 connection=connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  350.                                       prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_db_prep_value
  606.             value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
  613.         return int(value)

Exception Type: TypeError at /documents/
Exception Value: int() argument must be a string or a number, not 'Files'

1 个答案:

答案 0 :(得分:4)

您必须告诉序列化程序您希望从哪个模型字段获取值。

class DocumentSerializer(serializers.ModelSerializer):
    file_id = serializers.PrimaryKeyRelatedField(queryset=Files.objects.all(), 
                                                 source='file')

因为如果你不这样做,restframework会尝试使用序列化程序中定义的名称,因此它将使用file_id,而Documents模型中实际上并不存在。

# this is how PrimaryKeyRelatedField resolves its pk value
pk = getattr(obj, self.source or field_name).pk