gpt4 book ai didi

django信号模块对象没有属性connect

转载 作者:行者123 更新时间:2023-12-02 08:43:25 25 4
gpt4 key购买 nike

我正在尝试创建一个项目,用于在博客的帮助下创建用户的提要/事件提要。

这是信号.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(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)

当我尝试运行服务器时,出现错误:

AttributeError: 'module' object has no attribute 'connect'

请帮帮我。将不胜感激。谢谢。

最佳答案

替换

从 django.dispatch 导入调度程序 -> 从 django.dispatch 导入信号

dispatcher.connect -> Signal.connect

调度程序是模块,解释器告诉你。

关于django信号模块对象没有属性connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com