How to use primary key as an attribute

17 views
Skip to first unread message

Aamu Padi

unread,
Nov 30, 2013, 12:38:43 PM11/30/13
to django...@googlegroups.com
This may be a lame question, but how do I use the primary key as a default value for an attribute?

This is my models.py:

    class Thread(models.Model):
        thread_pk = models.PositiveIntegerField(default=self.pk)


This gives me an error:

    self is not defined

How do I go about using the pk as a default value of an attribute? Any help will be grateful. Thank you.

Timothy W. Cook

unread,
Nov 30, 2013, 5:05:43 PM11/30/13
to django...@googlegroups.com
I'm not sure what you are trying to accomplish. Since the 'pk' field
is going to be defined on your instances anyway, do you need a
'thread_pk' field?
> --
> 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/CAHSNPWs3NSQtzciqYL%2B7C7FL3Ybu1SAQv-L73EL2DNCMpJn7tA%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

Aamu Padi

unread,
Dec 1, 2013, 5:30:30 AM12/1/13
to django...@googlegroups.com
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?

Timothy W. Cook

unread,
Dec 1, 2013, 6:39:13 AM12/1/13
to django...@googlegroups.com
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
> --
> 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/CAHSNPWt14jxduA_kT-Yxj-7k02qZzZkvkbfYU2pEPFghbNq%3DEg%40mail.gmail.com.

Aamu Padi

unread,
Dec 1, 2013, 9:11:34 AM12/1/13
to django...@googlegroups.com
You are really are a life saver. Thank you so much sir. I have posted a more specific question. Please kindly have a look at it.
Thank you.


Reply all
Reply to author
Forward
0 new messages