suppose we have a model MarkPost. this model has ForeinKey relation to Post model.
假设我们有一个模型标记。该模型与后置模型具有显著的相关性。
class Post(models.Model):
TITLE = (
('1', 'USA'),
('2', 'Europe'), )
title = models.CharField(max_length=1, choices=TITLE)
class MarkPost(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE,
related_name="marked")
example:
例子:
foo_post = Post.objects.create(title="1")
marked_post1 = MarkPost.objects.create(user="Foo", post=foo_post)
I'm trying to write some signal notification that if another Post instance made with value marked_post1.post.title ,notify the related user in marked_post1.I made a signal function for this purpose.
我在写一些信号通知,如果另一个Post实例是用value marked_post1。Post创建的。标题,在marked_post1中通知相关用户。为此我做了一个信号函数。
signals.py
signals.py
def created_post(sender, instance, created, **kwargs):
marked = MarkPost.objects.get(pk=1)
marked_title = marked.post.title
if created and instance.title == marked_title :
#logic of my code
print(" new post made ")
post_save.connect(created_post, sender=Post)
this will work for one instance but how can do this in a general way for every user that marks a Post?
这对于一个实例来说是有用的,但是对于每一个标记Post的用户来说,如何做到这一点呢?
1 个解决方案
#1
1
Assuming that you have a defined logic behind the MarkPost
's id calculation,
假设您在MarkPost的id计算后面有一个已定义的逻辑,
def logic_to_find_markpost_id(req_data):
# do stuff to find MarkPost's id
return markpost_id
def created_post(sender, instance, created, **kwargs):
markpost_id = logic_to_find_markpost_id(req_data)
marked = MarkPost.objects.get(pk=markpost_id)
marked_title = marked.post.title
if created and instance.title == marked_title:
# logic of your code
print(" new post made ")
post_save.connect(created_post, sender=Post)
#1
1
Assuming that you have a defined logic behind the MarkPost
's id calculation,
假设您在MarkPost的id计算后面有一个已定义的逻辑,
def logic_to_find_markpost_id(req_data):
# do stuff to find MarkPost's id
return markpost_id
def created_post(sender, instance, created, **kwargs):
markpost_id = logic_to_find_markpost_id(req_data)
marked = MarkPost.objects.get(pk=markpost_id)
marked_title = marked.post.title
if created and instance.title == marked_title:
# logic of your code
print(" new post made ")
post_save.connect(created_post, sender=Post)