int()参数必须是字符串或数字,而不是'Files' [英] int() argument must be a string or a number, not 'Files'

查看:148
本文介绍了int()参数必须是字符串或数字,而不是'Files'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用模型的DjangoREST页面上传文件时,我遇到了一个奇怪的错误。以下是以下模型

I am getting an odd error when uploading a file using the DjangoREST page for a model. Here is the model below

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文件它给我的错误。

So when I hit submit with an attached PNG file it gives me the error.

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'


推荐答案

你必须告诉序列化器你期望哪个模型字段获取值。

You have to tell the serializer from which model field do you expect it to get the value from.

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

因为如果没有,restframework试图使用来自序列化程序的定义的名称,因此它将使用 file_id ,并且实际上不存在于 Documents 模型中。 p>

Because if you dont, restframework tries to use the defined name from the serializer so it will use file_id and that is actually not present in the Documents model.

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

这篇关于int()参数必须是字符串或数字,而不是'Files'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆