openplans / openblock

OpenBlock is a web application and RESTful service that allows users to browse and search their local area for "hyper-local news
61 stars 26 forks source link

Better abstraction of many-to-many Lookup attributes #258

Open slinkp opened 12 years ago

slinkp commented 12 years ago

Since m2m lookups are stored as a comma-separated string of ids, you can't really create or modify values conveniently. It would be nice if the AttributeDict made them look like a list of Lookup instances, or at least ids.

Then you wouldn't always have to write boilerplate code like:

        # Let's add one violation if it's not already there.
        ni = NewsItem.objects.get(...)
        violation_list = list(ni.attributes.get('violations', '').split(','))
        if str(violation.id) not in violation_list:
            violation_list.append(str(violation.id))
            ni.attributes['violations'] = ','.join(violation_list)

An even better solution would be to dispense with this silly comma-separated string entirely, and normalize the data properly, eg. with a many-to-many relationship between Lookups and Attributes. This would also mean we could get rid of the potentially slow regex search in NewsItemQuerySet.by_attribute(). It would mean more joins though.

slinkp commented 12 years ago

Ticket imported from Trac: http://developer.openblockproject.org/ticket/265 Reported by: slinkp