Update dates in models auto

10 views
Skip to first unread message

Juliano Araújo Farias

unread,
Jun 2, 2015, 10:38:14 AM6/2/15
to django...@googlegroups.com
Dears,

First I thx for ur attentions... and I novete in Django... but a have a question...

Second:
I have this in my model:

class Actionplan(models.Model):

    date_start_preview = models.DateField('Data Prevista Inicial', blank=True, null=True)
    date_end_preview = models.DateField('Data Prevista Final', blank=True, null=True)
    date_start = models.DateField('Data Real Inicial', blank=True, null=True)
    date_end = models.DateField('Data Real Final', blank=True, null=True)

class Actions(models.Model):

    date_start_preview = models.DateField('Data Prevista Inicial', blank=True)
    date_end_preview = models.DateField('Data Prevista Final', blank=True)
    date_start = models.DateField('Data Real Inicial', blank=True)
    date_end = models.DateField('Data Real Final', blank=True)

    actionplan = models.ForeignKey(Actionplan, verbose_name='Ações', related_name='actions')


-----------------------
and I want in admin do:

When the user create a action, the dates in actionplans update with the sum dates...


thx a lot...

Lachlan Musicman

unread,
Jun 2, 2015, 8:35:08 PM6/2/15
to django...@googlegroups.com
Over ride the save() method on teh Actions:


class Actions(models.Model):

    date_start_preview = models.DateField('Data Prevista Inicial', blank=True)
    date_end_preview = models.DateField('Data Prevista Final', blank=True)
    date_start = models.DateField('Data Real Inicial', blank=True)
    date_end = models.DateField('Data Real Final', blank=True)

    actionplan = models.ForeignKey(Actionplan, verbose_name='Ações', related_name='actions')

    def save(self, *args, **kwargs):
           # if you want the time "now"
           self.actionplan.date_start = datetime.datetime.now()
           # OR if you want the time to be the same as in the action:
           # self.actionplan.date_start = self.date_start
           # etc etc, do same for each var
           super(Actions, self).save(*args, **kwargs)


(check syntax if you need Timezones: https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/ , and here are the docs on method overriding:
https://docs.djangoproject.com/en/1.8/topics/db/models/#overriding-predefined-model-methods )

cheers
L.

------
let's build quiet armies friends, let's march on their glass towers...let's build fallen cathedrals and make impractical plans

- GYBE

--
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/2309e9e6-8289-4ea6-865e-9095bddb9fe8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages