I have a static Object called Module. One of the attributes for a Module object is a 'name'. I have a database table called Section that saves the Module key in a column.
I’d like to fetch the objects from that table and sort by the name of the Module they are related with.
Most of the time, when I reference the Module object, it’s like this: section.module.module_object.name
The model is:
class Section(models.Model):
page = models.ForeignKey(Page)
module = models.CharField(db_index=True, max_length=50, choices=[(k, '%s %s' % (s.service, s.name)) for k, s in MODULES.iteritems()])
I’ve tried this:
navigation_links = sorted(navigation_links.values(), key=lambda m: m['module'].module_object.name)
but I get the error AttributeError: 'unicode' object has no attribute 'module_object'
UPDATE: Here is the module object:
class Module(object):
def __init__(self, key, name):
self.key = key
self.name = name
# ... list of a lot of ALL_MODULES
#dict of the above list
MODULES = dict([(s.key, s) for s in ALL_MODULES])
From your code I infer that
MODULESis a dictionary of your Module objects, indexed by value by which they are saved in the database. These objects have anameproperty, so if you modify your example to fetch the object from this dictionary by saved name, you should get what you need:Of course, this is all based on my guesses about your code. If this doesn’t work, please provide more information about
sectionandmodule_objectvariables.