我正在使用Django来执行一个非常简单的功能。随着时间的推移,应用程序的大小会增加,但是现在,我想要做的就是运行我所有Facebook消息的html文档,并为每个用户保存一个模型,并为该用户附加每条消息。但是,在尝试创建我创建的模型的实例时,FacebookUser,我得到了错误" NoneType"对象不可调用。我阅读了其他SO文章,并浏览了互联网以寻找其他问题,并发现错误通常源于尝试对类本身做某事,而不是类的实例化。但是我正在创建这个类的实例,但仍然会收到此错误。
models.py
from django.db import models
class FacebookUser(models.Model):
full_name = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.full_name
class Message(models.Model):
user = models.ForeignKey(FacebookUser)
content = models.TextField(max_length=10000, null=True, blank=True)
date_sent = models.DateTimeField(null=True, blank=True)
def __str__(self):
return '#{} {}'.format(self.id, self.user.full_name)
views.py
from django.http import HttpResponse
from facebook_user.models import FacebookUser, Message
from bs4 import BeautifulSoup
def crawl_messages(request):
data = open('messages.html', 'r').read()
soup = BeautifulSoup(data)
all_messages = soup.findAll("div", {"class": "message"})
msg_dictionaries = [{
'user': el.findAll('span')[0],
'time': el.findAll('span')[1],
'content': el.nextSibling.nextSibling
} for el in all_messages]
for msg in msg_dictionaries:
try:
fbuser = FacebookUser.objects.get(full_name=msg['user'])
print('Try One')
print(fbuser)
except FacebookUser.DoesNotExist:
fbuser = FacebookUser.objects.create(full_name=msg['user'])
print('Try Two')
print(fbuser)
except Exception as e:
print('Try Three')
print(e)
fbuser = None
if fbuser:
new_msg = Message()
new_msg.content = msg['content']
new_msg.user = fbuser
new_msg.save()
return HttpResponse('Worked ! Check the Admin')
堆栈跟踪
Traceback (most recent call last):
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/timothybaney/Data Science/facebook_sentiment_analysis/facebook_user/views.py", line 19, in crawl_messages
fbuser = FacebookUser.objects.create(full_name=msg['user'])
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1059, in execute_sql
for sql, params in self.as_sql():
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1019, in as_sql
for obj in self.query.objs
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1019, in <listcomp>
for obj in self.query.objs
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1018, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 946, in prepare_value
value = value.resolve_expression(self.query, allow_joins=False, for_save=True)
TypeError: 'NoneType' object is not callable
答案 0 :(得分:2)
这很可能意味着msg['user']
不是字符串,而是在调用该属性时设置任何属性的对象。因此它会收到导致上述错误的resolve_expression
属性。
要修复,请尝试将msg['user']
转换为create语句中的字符串:
fbuser = FacebookUser.objects.create(str(msg['user'])
这只是一个猜测,没有完整的资源来运行它,无法确定。