Thank you all so much for the answers! I came up with this solution.

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 - %s" % (unicode(self.thread.id), self.body,
self.sender)

    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)

Please correct me, if this is not a good option. But with signals I can
check whether its a new created message or not. Does this have any draw
backs? Thank you once again!


On Tue, Dec 3, 2013 at 8:22 PM, antialiasis <antialia...@gmail.com> wrote:

> I believe what Aamu Padi is getting at is that he doesn't want to do this
> in the view at all. He just wants it to happen *whenever* a Message is
> created. That's a textbook use case for signals.
>
> On Tuesday, December 3, 2013 1:14:50 PM UTC, Timothy W. Cook wrote:
>>
>> On Tue, Dec 3, 2013 at 9:56 AM, Aamu Padi <aamu...@gmail.com> wrote:
>> > Ok! Plural names. Got it. But I was looking for something more of a
>> > overriding the save method for the Thread class, so that I don't have
>> to do
>> > that in the view.
>> >
>>
>> You can do it in the same view as the message.  You aren't limited to
>> working with only one model in a view.
>>
>> HTH,
>> Tim
>>
>>
>>
>>
>> >
>> > On Tue, Dec 3, 2013 at 1:39 PM, <jirka.v...@gmail.com> wrote:
>> >>
>> >> You're probably looking for something along the lines of:
>> >>
>> >> thread = Thread.objects.get(id=<someid>)
>> >> msg = Message.objects.get(id=<someid>)
>> >>
>> >> thread.user.add(msg.sender)
>> >>
>> >> Look at add() and remove() methods in documentation (search for
>> related
>> >> managers).
>> >>
>> >> FYI - it's a good practice to name fields that use ManyToMany field in
>> >> plural, i.e. Thread.users would be better than Thread.user
>> >>
>> >> HTH
>> >>
>> >> Jirka
>> >> ________________________________
>> >> From: Aamu Padi <aamu...@gmail.com>
>> >> Sender: django...@googlegroups.com
>> >> Date: Tue, 3 Dec 2013 04:19:55 +0530
>> >> To: <django...@googlegroups.com>
>> >> ReplyTo: django...@googlegroups.com
>> >> Subject: Save the user from one model to the another model
>> >>
>> >> 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 the Thread. How do I do
>> that?
>> >>
>> >>>
>> >>> 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)
>> >>>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> Groups
>> >> "Django users" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> an
>> >> email to django-users...@googlegroups.com.
>> >> To post to this group, send email to django...@googlegroups.com.
>> >> Visit this group at http://groups.google.com/group/django-users.
>> >> To view this discussion on the web visit
>> >> https://groups.google.com/d/msgid/django-users/
>> CAHSNPWveL8rqDLenDLayGrtC_RAwQ-Pfp0SYravr7%2BuJzhBhXw%40mail.gmail.com.
>> >> For more options, visit https://groups.google.com/groups/opt_out.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> Groups
>> >> "Django users" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> an
>> >> email to django-users...@googlegroups.com.
>> >> To post to this group, send email to django...@googlegroups.com.
>> >> Visit this group at http://groups.google.com/group/django-users.
>> >> To view this discussion on the web visit
>> >> https://groups.google.com/d/msgid/django-users/495305343-
>> 1386058174-cardhu_decombobulator_blackberry.rim.net-1176089190-%40b17.c3.
>> bise7.blackberry.
>> >> For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "Django users" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an
>> > email to django-users...@googlegroups.com.
>> > To post to this group, send email to django...@googlegroups.com.
>> > Visit this group at http://groups.google.com/group/django-users.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msgid/django-users/CAHSNPWtJ80rqeDgJU4x3xk%
>> 2B3ABR1SKjA%2BohsPa3cPezx8O%3Darg%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>> --
>> MLHIM VIP Signup: http://goo.gl/22B0U
>> ============================================
>> Timothy Cook, MSc           +55 21 94711995
>> MLHIM http://www.mlhim.org
>> Like Us on FB: https://www.facebook.com/mlhim2
>> Circle us on G+: http://goo.gl/44EV5
>> Google Scholar: http://goo.gl/MMZ1o
>> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7e351773-8a47-4c04-8795-6d71e4746bf0%40googlegroups.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsGxZw3Eww8rr944-K4F68q7R22tJAwSbVS1%3Dmr-1XYfA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to