Permalink
Browse files

Changing location type relationship to m2m

  • Loading branch information...
1 parent feb1762 commit 1b221c21d38b0f28b2a987ccd862bf7baed1c6dc Mark Ransom committed Mar 1, 2012
View
@@ -2,6 +2,7 @@
*.vim
*.swp
*.swo
+*.swn
*.py~
*.html~
*.txt~
View
@@ -11,7 +11,7 @@ class LocationAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
- 'fields': ('name','location_type',)
+ 'fields': ('name','location_types',)
}),
('Address', {
'fields': ('address', ('latitude', 'longitude'))
@@ -0,0 +1,78 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Deleting field 'Location.location_type'
+ db.delete_column('store_locator_location', 'location_type_id')
+
+ # Adding M2M table for field location_types on 'Location'
+ db.create_table('store_locator_location_location_types', (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('location', models.ForeignKey(orm['store_locator.location'], null=False)),
+ ('locationtype', models.ForeignKey(orm['store_locator.locationtype'], null=False))
+ ))
+ db.create_unique('store_locator_location_location_types', ['location_id', 'locationtype_id'])
+
+
+ def backwards(self, orm):
+
+ # Adding field 'Location.location_type'
+ db.add_column('store_locator_location', 'location_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['store_locator.LocationType'], null=True, blank=True), keep_default=False)
+
+ # Removing M2M table for field location_types on 'Location'
+ db.delete_table('store_locator_location_location_types')
+
+
+ models = {
+ 'cms.cmsplugin': {
+ 'Meta': {'object_name': 'CMSPlugin'},
+ 'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.CharField', [], {'max_length': '15', 'db_index': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.CMSPlugin']", 'null': 'True', 'blank': 'True'}),
+ 'placeholder': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Placeholder']", 'null': 'True'}),
+ 'plugin_type': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
+ 'position': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ 'cms.placeholder': {
+ 'Meta': {'object_name': 'Placeholder'},
+ 'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'store_locator.location': {
+ 'Meta': {'object_name': 'Location'},
+ 'address': ('django.db.models.fields.TextField', [], {'max_length': '255'}),
+ 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'latitude': ('django.db.models.fields.FloatField', [], {'null': 'True'}),
+ 'location_types': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['store_locator.LocationType']", 'null': 'True', 'blank': 'True'}),
+ 'longitude': ('django.db.models.fields.FloatField', [], {'null': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'phone': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'})
+ },
+ 'store_locator.locationtype': {
+ 'Meta': {'object_name': 'LocationType'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'store_locator.storelocator': {
+ 'Meta': {'object_name': 'StoreLocator', 'db_table': "'cmsplugin_storelocator'", '_ormbases': ['cms.CMSPlugin']},
+ 'cmsplugin_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['cms.CMSPlugin']", 'unique': 'True', 'primary_key': 'True'}),
+ 'default_distance': ('django.db.models.fields.CharField', [], {'default': "'10'", 'max_length': '50'}),
+ 'starting_location': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ }
+ }
+
+ complete_apps = ['store_locator']
View
@@ -56,7 +56,7 @@ def __unicode__(self):
return self.name
class Location(models.Model):
- location_type = models.ForeignKey(LocationType, blank=True, null=True)
+ location_types = models.ManyToManyField(LocationType, blank=True, null=True)
name = models.CharField(max_length=255)
address = models.TextField(max_length=255, blank=False)
latitude = models.FloatField(blank=False, null=True)
View
@@ -26,7 +26,7 @@ def get_locations(request):
locations = Location.objects.near(latitude, longitude, distance)
if location_type:
- locations = [l for l in locations if str(l.location_type_id) == location_type]
+ locations = [l for l in locations if location_type in [str(t[0]) for t in l.location_types.values_list('id')]]
json_locations = []
for location in locations:
location_dict = {}

0 comments on commit 1b221c2

Please sign in to comment.