Adding code when pressing one of admin's save buttons

77 views
Skip to first unread message

ron_...@yahoo.co.uk

unread,
Jul 25, 2017, 8:53:49 AM7/25/17
to Django users

I am using Django’s admin interface to create new records. A record includes (amongst other fields) the date and time that a photo was taken. These fields will be filled into the database when either the ‘Save and add another’, ‘Save and continue editing’ or the ‘Save’ buttons are pressed (i.e. these details will be read from the image file and entered into the database rather than the administrator doing this manually).

 

How can I added the extra code required to add the date and time when I press one of these buttons? Some example code would be appreciated.

 

Thanks

Thiago Luiz Parolin

unread,
Jul 25, 2017, 9:30:02 AM7/25/17
to django...@googlegroups.com
Try using 'def save(self):' on your models.
When you save a new record, this will be executed, so if you hit any button, you are saving in anyway and the code will be executed.

Just my cents about your question.


--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/63559d9c-15c6-4ce1-b477-8e24b030ce0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Jul 25, 2017, 7:00:43 PM7/25/17
to django...@googlegroups.com
class MyModel(models.Model):

...
photodatetime = models.DateTimeField()
...

def save(self, *args, **kwargs):
photodatetime = self.getphotoinfo()
super(MyModel, self).save(*args, **kwargs)

def getphotoinfo(self):
...




> Thanks
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/63559d9c-15c6-4ce1-b477-8e24b030ce0c%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages