Sunday, December 1, 2013

Re: How to use primary key as an attribute

If I understand what you are building. You want some way to group
messages into threads? I assume that a Thread will have many messages?
Unless I am missing something you might want something like this:

class Thread(models.Model):
subject = models.CharField(max_length=256)

class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
body = models.TextField()
user = models.ForeignKey(User)

Then you can create a filter for the Thread based on a subject:

selected_subject = Thread.objects.filter(subject="some subject")

Then when you create the message you can use thread =
selected_subject[0] (assuming unique subjects/threads) when you create
your message. OR if 'some subject' isn't found, len(selected_subject)
== 0 then create a new Thread.

HTH,
Tim



On Sun, Dec 1, 2013 at 8:30 AM, Aamu Padi <aamupadi@gmail.com> wrote:
> Actually I need to use Thread class in other class as a ForeignKey. Here is
> the whole code:
>
> class Thread(models.Model):
> thread_pk = models.PositiveIntegerField(default=self.pk)
>
> class ThreadParticipant(models.Model):
> thread = models.ForeignKey(Thread)
> user = models.ForeignKey(User)
>
> class Message(models.Model):
> thread = models.ForeignKey(Thread)
> sent_date = models.DateTimeField(default=datetime.now)
> body = models.TextField()
> user = models.ForeignKey(User)
>
>
> Is there anyway to create a new message without manually creating a Thread.
> I mean to auto-populate the Thread, whenever I create a new Message?
>
> --
> 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+unsubscribe@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/CAHSNPWt14jxduA_kT-Yxj-7k02qZzZkvkbfYU2pEPFghbNq%3DEg%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+unsubscribe@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/CA%2B%3DOU3XsrpZ5U-2626-r4dTvQgoeXUiKY%3Dvx1Yp5dPjHKYnr0A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment