From 2d8849272af6229dfc3e1fd7cde6e0471e3d4486 Mon Sep 17 00:00:00 2001 From: "Jonathan S. Katz" Date: Tue, 29 Nov 2011 13:06:38 -0500 Subject: [PATCH] Create prettier text for many-to-many fields in mail notifications Creates prettier text for many-to-many fields for mail notifications by detecting if such a field is of a many-to-many type, and if so, joins the list together based on the related object's default unicode representation Fixes #110 removed extra line of commented code --- pgweb/util/bases.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pgweb/util/bases.py b/pgweb/util/bases.py index 83f47d59..73974896 100644 --- a/pgweb/util/bases.py +++ b/pgweb/util/bases.py @@ -101,7 +101,11 @@ class PgModel(object): def _get_attr_value(self, fieldname): try: - return getattr(self, fieldname) + # see if this is a Many-to-many field, if yes, we want to print out a pretty list + value = getattr(self, fieldname) + if isinstance(self._meta.get_field_by_name(fieldname)[0], models.ManyToManyField): + return ", ".join(map(lambda x: unicode(x), value.all())) + return value except ValueError, v: # NOTE! If the object is brand new, and it has a many-to-many relationship, we can't # access this data yet. So just return that it's not available yet. @@ -132,9 +136,9 @@ class PgModel(object): s = "\n\n".join(["%s from: %s\n%s to: %s" % ( n, - getattr(oldobj, n), + oldobj._get_attr_value(n), n, - getattr(self, n), + self._get_attr_value(n), ) for n in fieldlist if not getattr(oldobj,n)==getattr(self,n)]) if not s: return None return s -- 2.39.5