Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>save</code> method or by using a <code>post_save</code> signal.</p> <p><code>save</code> is good enough for almost all cases - the advantage of <code>post_save</code> 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 <code>save</code> can work fine:</p> <pre><code>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) </code></pre> <p>A signal would look like this:</p> <pre><code>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) </code></pre> <p>And then review the docs on preventing duplicate signals in case of multiple imports: <a href="https://docs.djangoproject.com/en/dev/topics/signals/#preventing-duplicate-signals" rel="nofollow">https://docs.djangoproject.com/en/dev/topics/signals/#preventing-duplicate-signals</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload