I managed to solve the issue. Here's the code:
def save(self, *args, **kwargs):
if hasattr(self, 'id') and getattr(self, 'id') is not None:
old_issue = Issues.objects.get(id=getattr(self,'id'))
self.updated_at = datetime.datetime.now()
if old_issue.issue != self.issue:
self.issue = self.issue +' '+':' + 'Modified' + ' ' +
str(datetime.datetime.now())
super(Issues, self).save(*args, **kwargs)
the table is Issues and has a field called issue. Having a record open
I get its id and then a copy of the record in the database. I compare
to the record as it is in the admin screen. If they are the same I do
not append the date (there is certainly a more elegant way of
appending the date than what I have here) and if not no date is
appended. This works as I intend.
On Apr 17, 11:25 am, Aref <[email protected]> wrote:
> Thank you Mengu. I have tried what you suggested (I was concatenating
> the date to the updated field instead) but the problem is that every
> time there is a save the date gets added even though the field has not
> changed. So I wanted to test if the field was changed and if so
> concatenate the date to it if not I would do nothing.
> Thanks for taking the time to respond to my question. Much
> appreciated.
>
> On Apr 17, 11:01 am, Mengu <[email protected]> wrote:
>
> > i believe it does. you can override the "save" method of your model
> > like this:
>
> > from django.db import models
> > from datetime import datetime
>
> > # Create your models here.
> > class TestModel(models.Model):
> > first_field = models.CharField(max_length=255)
> > second_field = models.BooleanField()
> > updated_at = models.DateTimeField(default=datetime.now)
>
> > def save(self, *args, **kwargs):
> > if hasattr(self, 'id') and getattr(self, 'id') is not None:
> > self.updated_at = datetime.now()
> > super(TestModel, self).save(*args, **kwargs)
>
> > On Apr 17, 6:25 pm, Aref <[email protected]> wrote:
>
> > > Hello,
>
> > > I have a text field which could be updated regularly and I want to
> > > automatically attach a date every time the record is updated--but only
> > > if the record is updated. Can this be done in django and how would I
> > > go about doing it.
> > > Thanks.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.