상태에서 사용자의 활동 스트림을 만들려고합니다.django - 신호가 작동하지 않습니다.
모델 : 나는 새 상태를 만들 수 있지만
그러나class Status(models.Model):
body = models.TextField(max_length=200)
image = models.ImageField(blank=True, null=True, upload_to=get_upload_file_name)
privacy = models.CharField(max_length=1,choices=PRIVACY, default='F')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
user = models.ForeignKey(User)
class Activity(models.Model):
actor = models.ForeignKey(User)
action = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
, 그것은 post_save
신호로부터 새로운 활동을 만들지 않습니다.
신호 : 내가 잘못 뭐하는 거지
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import post_save
from status.models import Status
from models import Activity
def create_activity_item(sender, instance, signal, *args, **kwargs):
if kwargs.get('created', True):
ctype = ContentType.objects.get_for_model(instance)
if ctype.name == 'Status':
action = ' shared '
activity = Activity.objects.get_or_create(
actor = instance.user,
action = action,
content_type = ctype,
object_id = instance.id,
pub_date = instance.pubdate
)
post_save.connect(create_activity_item, sender=Status)
? 이 문제를 해결하도록 도와주세요. 나는 매우 감사 할 것입니다. 고맙습니다.
는 업데이트 :
그러나이 같은 일이 활동을 만듭니다
@receiver(post_save, sender=Status)
def create(sender, instance, **kwargs):
if kwargs.get('created',True):
ctype = ContentType.objects.get_for_model(instance)
activity = Activity.objects.get_or_create(
actor = instance.user,
action = ' shared ',
content_type = ctype,
object_id = instance.id,
pub_date = instance.pub_date
)
하지 않는 이유는 다음 위의 작품?
안녕하세요. 나는 대답에서 한 것처럼 행동했다. 그러나 여전히 활동을 생성하지는 않습니다. '__init __. py'에서'default_app_config = 'activities.apps.ActivityAppConfig''를 추가했습니다. activities는 앱의 이름입니다. 'ActivityAppConfig'의 이름 필드를'activities '로 변경하면서 app에 새로운 apps.py 파일을 추가했습니다. 그런 다음'dispatch_uid'를 추가하십시오. – Kakar
업데이트 된 답변보기 – catavaran
예! 이제 그 일. 하나 더. apps.py를 생성하여 항상 신호를 가져와야합니까? 그것 없이는 왜 작동하지 않습니까? – Kakar