Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django序列化程序错误:';非类型';对象没有属性'_meta&x27;_Django_Json_Serialization_Attributeerror - Fatal编程技术网

Django序列化程序错误:';非类型';对象没有属性'_meta&x27;

Django序列化程序错误:';非类型';对象没有属性'_meta&x27;,django,json,serialization,attributeerror,Django,Json,Serialization,Attributeerror,我最近在Django中找到了大量JSON序列化各种对象的代码。不幸的是,代码遇到某些类型的模型时会抛出AttributeError 以下是我试图诊断和解决的错误和回溯: AttributeError at /serial/ 'NoneType' object has no attribute '_meta' Request Method: GET Request URL: http://127.0.0.1:8000/serial/ Django Version: 1.2.5 Excepti

我最近在Django中找到了大量JSON序列化各种对象的代码。不幸的是,代码遇到某些类型的模型时会抛出AttributeError

以下是我试图诊断和解决的错误和回溯:

AttributeError at /serial/
'NoneType' object has no attribute '_meta'
Request Method: GET
Request URL:    http://127.0.0.1:8000/serial/
Django Version: 1.2.5
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute '_meta'
Exception Location: /Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py in handle_m2m_field, line 183
Python Executable:  /usr/bin/python
Python Version: 2.6.7
Python Path:    ['/Users/jphill/apps/d_projects/smartgoal', '/Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Python/2.6/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC']
Server time:    Fri, 11 Nov 2011 23:08:13 -0500

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/serial/
Django Version: 1.2.5
Python Version: 2.6.7
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'hq']
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')


Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/Users/jphill/apps/d_projects/smartgoal/hq/views.py" in serial
  164.      return json_response_from(Task.objects.all())
File "/Users/jphill/apps/d_projects/smartgoal/hq/views.py" in json_response_from
  88.     return HttpResponse(jsonSerializer.serialize(response, ), mimetype='application/json')
File "/Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py" in serialize
  34.         self.handle_object(obj)
File "/Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py" in handle_object
  75.             self.handle_queryset(object)
File "/Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py" in handle_queryset
  138.             self.handle_model(mod)
File "/Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py" in handle_model
  128.                     self.handle_m2m_field(mod, field)
File "/Users/jphill/apps/d_projects/smartgoal/../smartgoal/hq/custom_serializer.py" in handle_m2m_field
  183.         if field.rel.through._meta.auto_created:

Exception Type: AttributeError at /serial/
Exception Value: 'NoneType' object has no attribute '_meta' 
这是序列化程序代码(不是我的,在这里找到)

以下是我尝试序列化的模型:

class Task(models.Model):
    """
    Model used for tracking tasks
    """
    PRIORITY_CHOICES = (
        ('0', 'None'),
        ('1', 'High'),
        ('2', 'Medium'),
        ('3', 'Low'),
    )
    name = models.CharField(max_length=255)
    completed = models.BooleanField(default=False)
    hidden = models.BooleanField(default=False)
    timestamp = models.DateField(auto_now=True)
    priority = models.CharField(default=0, max_length=1, choices=PRIORITY_CHOICES)
    creator = models.ForeignKey(User, related_name="created_task")
    # optional
    owner = models.ForeignKey(User, related_name="owned_task", blank=True, null=True)
    goal = models.ForeignKey('Goal', blank=True, null=True)
    reminder = models.DateTimeField(blank=True, null=True)
    note = models.TextField(blank=True)
    started = models.BooleanField(default=False, blank=True)
    activities = generic.GenericRelation('Activity')
下面是我在views.py中执行所有操作的方式:

def serial(request):
     return json_response_from(Task.objects.all())

def json_response_from(response):
    jsonSerializer = JSONSerializer()
    return HttpResponse(jsonSerializer.serialize(response, use_natural_keys=True), mimetype='application/json')

我猜这是
一般关系的问题。这是一种全新的字段类型,可能在编写序列化代码时没有实现

GenericRelation字段在内部使用了许多关系,可能是因为它们没有通过
对象定义
字段.rel。这会导致您的案例中出现错误。您可以通过添加一些日志来验证这一点,以查看哪个字段导致了问题

要解决此问题,您可以进一步查看
泛型关联
,并修改序列化代码以支持它们,或者使用其他支持
泛型关联
的序列化技术

来自Django源代码的剪辑,用于
generirelation
class:

    def get_internal_type(self):
      return "ManyToManyField"

我想就是这样。非常感谢。
    def get_internal_type(self):
      return "ManyToManyField"