将用户从一个模型保存到另一个模型 [英] Save the user from one model to the another model
问题描述
我想做的是,每当我创建一个新消息时,我想要消息
的发件人
被添加到该特定的
(用户
线程消息
其相关)。
我该怎么做?可以通过覆盖 save
方法来实现吗?我可以在views.py中执行,但是我希望如果我可以将它添加到models.py本身中会更好。任何帮助将非常感谢。谢谢!
class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User,related_name ='hidden_thread',blank = True)
def __unicode __(self):
return unicode(self.id)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default = datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User,related_name ='hidden_message',blank = True)
def __unicode __(self):
返回%s - %s%(unicode(self.thread.id),self.body)
如果您不想主动将用户设置为dm03514显示,例如,如果要在默认情况下将用户添加到线程,但保持从线程多对多以后,你可以做到这通过覆盖保存
方法或使用 post_save
信号。
$ b $对于几乎所有的情况,b
save
是足够好的 - post_save
的优点在于它可以更可靠地区分保存新消息并保存对现有消息的编辑。但是如果您没有使用预先选择的PK创建消息,或者从灯具加载它们 save
可以正常工作:
<$ p $
def save(self,* args,** kwargs):
might_new =(self.pk is None)
super(Message,self).save(* args,** kwargs)
如果might_new:
self.thread.user.add(self.sender)
信号如下所示:
django.db.models.signals import post_save
def update_thread_users(sender,** kwargs):
created = kwargs ['created']
raw = kwargs ['raw' ]
如果创建而不是raw:
instance = kwargs ['instance']
instance.thread.user.add(instance.sender)
post_save.connect (update_thread_users,sender = Message)
然后在多次导入时检查防止重复信号的文档:
<啊ref =https://docs.djangoproject.com/en/dev/topics/signals/#preventing-duplicate-signals =nofollow> https://docs.djangoproject.com/en/dev/topics/信号/#防止重复信号
What I want to do is, whenever I create a new message, I want the sender
of the Message
to be added to the user
of that particular Thread
(the Message
its relating to).
How do I do that? Can it be done by overriding the save
method? I can do it in the views.py, but I was hoping it would be better if I can add it in the models.py itself. Any help will be very grateful. Thank you!
class Thread(models.Model):
user = models.ManyToManyField(User)
is_hidden = models.ManyToManyField(User, related_name='hidden_thread', blank=True)
def __unicode__(self):
return unicode(self.id)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
sender = models.ForeignKey(User)
body = models.TextField()
is_hidden = models.ManyToManyField(User, related_name='hidden_message', blank=True)
def __unicode__(self):
return "%s - %s" % (unicode(self.thread.id), self.body)
If you don't want to actively pull the user set as dm03514 showed, such as if you want to add users to the thread by default but maintain the ability to remove them from the thread many-to-many later, you can indeed do this by overriding the save
method or by using a post_save
signal.
save
is good enough for almost all cases - the advantage of post_save
is that it can more reliably distinguish between saving a new message and saving edits to an existing message. But if you're not creating messages with preselected PKs or loading them from fixtures save
can work fine:
class Message(models.Model):
def save(self, *args, **kwargs):
probably_new = (self.pk is None)
super(Message, self).save(*args, **kwargs)
if probably_new:
self.thread.user.add(self.sender)
A signal would look like this:
from django.db.models.signals import post_save
def update_thread_users(sender, **kwargs):
created = kwargs['created']
raw = kwargs['raw']
if created and not raw:
instance = kwargs['instance']
instance.thread.user.add(instance.sender)
post_save.connect(update_thread_users, sender=Message)
And then review the docs on preventing duplicate signals in case of multiple imports: https://docs.djangoproject.com/en/dev/topics/signals/#preventing-duplicate-signals
这篇关于将用户从一个模型保存到另一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!