Hi All,
I have a model CampaignYear that is registered with django-reversion so that
when a CampaignYear instance is saved a django-reversion Version object is
also saved. This worked great until I wanted to do the following:
add a field to CampaignYear, last_editor(), that queries the latest Version
object related to my CampaignYear instance and returns a string combining the
user, date, and comment associated with that version. My code for this is
below:
def last_editor(self):
# Get latest version from reversion
version = Version.objects.get_for_date(self, datetime.datetime.now())
# Get data from version
user = version.revision.user
date_strf = version.revision.date_created.strftime("%b %d, %I:%M%p")
comment = version.revision.comment
last_editor = (u'%s : %s - %s' % (user, date_strf, comment))
return last_editor
This works, but is very database intensive and slow. So I want to create a
CharField in CampaignYear called last_editor_str that contains the value
returned by last_editor(), so that this computation is only done when I save
the object, rather than every time I want to view it.
My problem is here: the Version object associated with each CampaignYear isn't
created until after I save CampaignYear. So I need to save CampaignYear once
and then save it again with last_editor_str populated from my last_editor()
function.
So my question is how should I go about doing this double save?
One thing I tried was using
super(CampaignYear, self).save(*args, **kwargs)
twice in CampaignYear.save(). Once before last_editor_str is populated and
then again after. This didn't work. I don't know if you can use super().save()
twice?
I also tried using signals:
def camp_year_post_save(sender, **kwargs):
if (sender.last_editor_str == ''):
try:
sender.last_editor_str = sender.get_last_editor()
sender.save()
except Version.DoesNotExist:
pass
post_save.connect(camp_year_post_save, sender=CampaignYear)
However, this threw a
AttributeError: type object 'CampaignYear' has no attribute 'last_editor_str'
error.
If I can clarify anything please let me know,
Thanks,
Evan
--
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.