Sunday, December 1, 2013

Re: auto populating the fields in the models

sorry my previous message... it was not quite clear... here again:



2013/12/1 Rafael E. Ferrero <rafael.ferrero@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:
 

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 <aamupadi@gmail.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.

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



--
Rafael E. Ferrero



--
Rafael E. Ferrero

--
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/CAJJc_8VJCH01_SQZKOkAz%3DhwHgC%3Dsj-w%3DKLQTxJc1K92RZ11jw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment