2013-11-27 2 views
0

모델의 DjangoREST 페이지를 사용하여 파일을 업로드 할 때 이상한 오류가 발생합니다. 여기에 여기에int() 인수는 'Files'가 아닌 문자열 또는 숫자 여야합니다.

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' 
+0

전체 추적을 표시하십시오. – jbub

+0

@jbub - 내 게시물을 편집했습니다. – thedemon

답변

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 
+0

그게 다야! 도와 주셔서 감사합니다. – thedemon

관련 문제