I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog.
我正在尝试创建一个项目,用于在博客的帮助下创建用户的订阅源/活动订阅源。
These are the models -
这些是模特 -
class StreamItem(models.Model):
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
pub_date = models.DateTimeField(default=datetime.now)
content_object = generic.GenericForeignKey('content_type', 'object_id')
@property
def content_class(self):
return self.content_type.model
class Blog(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=300)
body = models.TextField()
pub_date = models.DateTimeField(default=datetime.now)
class Photo(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=200)
image = models.ImageField(upload_to=get_upload_file_name)
pub_date = models.DateTimeField(default=datetime.now)
And this is the signals.py:
这就是signals.py:
__init__.py
from django.db.models import signals
from django.contrib.contenttypes.models import ContentType
from django.dispatch import dispatcher
from blogs.models import Blog
from picture.models import Photo
from models import StreamItem
def create_stream_item(sender, instance, signal, *args, **kwargs):
# Check to see if the object was just created for the first time
if 'created' in kwargs:
if kwargs['created']:
create = True
# Get the instance's content type
ctype = ContentType.object.get_for_model(instance)
if create:
si = StreamItem.objects.get_or_create(user=instance.user, content_type=ctype, object_id=instance.id, pub_date = instance.pub_date)
# Send a signal on post_save for each of these models
for modelname in [Blog, Photo]:
dispatcher.connect(create_stream_item, signal=signals.post_save, sender=modelname)
When I create a blog or upload a photo, the signal
does not work. And I am not getting any error too. But I can manually add items to the StreamItem
app using the admin, and the StreamItem does work as I want it to be. I think there's problem with the signals.py. Please help me out. Would be much appreciate. Thank you.
当我创建博客或上传照片时,信号不起作用。我也没有收到任何错误。但是我可以使用admin手动将项目添加到StreamItem应用程序,StreamItem可以按照我的意愿运行。我认为signals.py存在问题。请帮帮我。非常感谢。谢谢。
2 个解决方案
#1
11
You have to make sure that the signals are loaded soon after django is started. The one possible way to ensure it is to import the module into __init__.py
您必须确保在启动django后很快加载信号。确保它的一种可能方法是将模块导入__init__.py
# __init__.py
# add the below line and run the project again
import signals
#2
0
Unless you've omitted code, the new si
item your signal handler creates is missing the required field user
. You probably need to add that to your get_or_create
call.
除非您省略了代码,否则信号处理程序创建的新si项目缺少必需的字段用户。您可能需要将其添加到get_or_create调用中。
#1
11
You have to make sure that the signals are loaded soon after django is started. The one possible way to ensure it is to import the module into __init__.py
您必须确保在启动django后很快加载信号。确保它的一种可能方法是将模块导入__init__.py
# __init__.py
# add the below line and run the project again
import signals
#2
0
Unless you've omitted code, the new si
item your signal handler creates is missing the required field user
. You probably need to add that to your get_or_create
call.
除非您省略了代码,否则信号处理程序创建的新si项目缺少必需的字段用户。您可能需要将其添加到get_or_create调用中。