Sigh.  I hate Google Groups via web.  Here is a readable version of
the .save() method below.

https://gist.github.com/1591028

On Jan 10, 3:15 pm, Jeff <jbla...@mitre.org> wrote:
> For example, altering 'pana.our.org''s netgroups field, the following
> save() method results in this debug line which makes no sense to me:
>
>     CHANGED: Device pana.our.org had old netgroups [{'name':
> u'testnetgroup', 'desc': u''}] and now has new netgroups [{'name':
> u'testnetgroup', 'desc': u''}]
>
> Both the new and old netgroups in that line are identical and reflect
> the NEW value.  Working code would state:
>
>     CHANGED: Device pana.our.org had old netgroups [{'name':
> u'testnetgroup', 'desc': u''}] and now has new netgroups []
>
> class Device(models.Model):
>     # ...
>     netgroups       = models.ManyToManyField(Netgroup,
>                                              null=True,
>                                              verbose_name="Netgroups",
>                                              blank=True)
>     # ...
>     def save(self, *args, **kwargs):
>         try:
>             #
>             # Get the THEORETICALLY old data from database for this
> device
>             #
>             oldnetgroups =
> Device.objects.get(pk=self.pk).netgroups.values_list()
>         except:
>             # This is likely a brand new device object, so can't look
> it up
>             oldnetgroups = None
>         #
>         # Compare the THEORETICAL old netgroups value with the
> theoretical new value
>         #
>         if (oldnetgroups != None) and (oldnetgroups !=
> self.netgroups.values_list()):
>             prefix = 'CHANGED'
>         else:
>             prefix = 'UNCHANGED'
>         if oldnetgroups != None:
>             fd = open('/tmp/test.log', 'a')
>             fd.write('%s: Device %s had old netgroups %s and now has
> new netgroups %s\n' % (prefix, self.name, oldnetgroups,
> self.netgroups.values()))
>             fd.close()
>         super(Device, self).save(*args, **kwargs)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to