Tôi đang cố tạo luồng hoạt động của người dùng từ trạng thái của họ.django - tín hiệu không hoạt động
mô hình:
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)
Tuy nhiên, mặc dù tôi tạo ra một trạng thái mới, nó không tạo ra một hoạt động mới từ tín hiệu post_save
.
tín hiệu:
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)
Tôi đang làm gì sai? Xin hãy giúp tôi giải quyết vấn đề này. Tôi sẽ rất biết ơn. Cảm ơn bạn.
Cập nhật:
Tuy nhiên làm như thế này tạo ra hoạt động:
@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
)
Tại sao không phải là công trình trên sau đó?
hello. Tôi đã làm như bạn đã làm trong câu trả lời. Nhưng nó vẫn không tạo ra bất kỳ hoạt động nào. Trong '__init __. Py' tôi đã thêm' default_app_config = 'activities.apps.ActivityAppConfig'' làm hoạt động là tên của ứng dụng. Và sau đó thêm tệp apps.py mới trong ứng dụng trong khi thay đổi trường tên của 'ActivityAppConfig' thành 'hoạt động'. Và sau đó thêm 'dispatch_uid'. – Kakar
Xem câu trả lời được cập nhật. – catavaran
Có! Bây giờ nó hoạt động. Một điều nữa. Tôi có luôn phải nhập tín hiệu bằng cách tạo apps.py không? Tại sao nó không hoạt động mà không có nó? – Kakar