auto populating the fields in the models

30 views
Skip to first unread message

Aamu Padi

unread,
Dec 1, 2013, 9:13:23 AM12/1/13
to django...@googlegroups.com

Hello, please have a look at my models.py.

models.py:

class Thread(models.Model):
    pass

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)

class MessageReadState(models.Model):
    message = models.ForeignKey(Message)
    user = models.ForeignKey(User)
    read_date = models.DateTimeField()

I am having two problems when I try to create a new message:

  1. How do I auto populate the Thread with its primary key whenever I create a new message, without manually creating a new thread?
  2. How to create a new user if the user is not in the the ThreadParticipant, or else don't create a new user?

I think I can solve this all in the views.py, but I think it will be better to solve this in the models.py. Please help me solve the problem. I would be very grateful. Thank you.

Rafael E. Ferrero

unread,
Dec 1, 2013, 10:39:53 AM12/1/13
to django...@googlegroups.com
IMHO with this design you first need an user and a thread to create a new message.

In the admin interface you gonna look a + button to create a user and other to create a thread.

For example

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

    def save(self):
        if not self.id:
            #on a new message do something
        #on modify message do somenthing else
        super(Message, self).save()






2013/12/1 Aamu Padi <aamu...@gmail.com>

--
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/CAHSNPWsLC1LW1pqpYe-FUmC4k4twxBV8HrLxRex4e6VNicoBdQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

Rafael E. Ferrero

unread,
Dec 1, 2013, 10:44:57 AM12/1/13
to django...@googlegroups.com
sorry my previous message... it was not quite clear... here again:



2013/12/1 Rafael E. Ferrero <rafael....@gmail.com>

IMHO with this design you first need an user and a thread to create a new message.

In the admin interface you gonna look a + button to create a user and other to create a thread.


OR

You can redefine the save method of your model. For example:
 



--
Rafael E. Ferrero

Aamu Padi

unread,
Dec 1, 2013, 11:16:06 AM12/1/13
to django...@googlegroups.com
Yes, that's what I was looking for, the save method. Can you please kindly show me how, so that I don't have to create a new thread first or create a new set of ThreadParticipants. I just want it to be on the background, so that all I have to do is create a message and send it to the user.
Thank you.


Reply all
Reply to author
Forward
0 new messages