-- Country import insert into common_country(id,name,code,population,latitude,longitude,alternatenames) select geonameid as id,name,country as code, population,latitude,longitude,alternatenames from geonames.geoname gn where (gn.fcode IN ('PCLI','PCLIX','PCLD','PCLS','PCLF','PCL','TERR')); delete from common_country where id in (2077507,2170371,7910334,7909807); create unique index common_country_idx on common_country (id); create index common_country_code_idx on common_country(code); -- South Korea Fix update common_country set alternatenames = concat(alternatenames,"Korea, Republic of") where id = 1835841; -- City import insert into common_city(id,name,country_id,alternatenames,latitude,longitude, adm) select gn.geonameid as id, gn.name, c.id as country_id, gn.alternatenames, gn.latitude, gn.longitude, admin1 as adm from geonames.geoname gn left join common_country as c on gn.country=c.code where (gn.fcode in ('PPL','PPLC','PPLA','PPLA2','PPLA3','PPLA4'));
source geo_city { type = mysql sql_host = localhost sql_user = citylist sql_pass = citylist sql_db = citylist sql_port = sql_query_pre = SET NAMES utf8 sql_query_post = sql_query = \ SELECT id, name, country_id, alternatenames, latitude, longitude\ FROM geo_city sql_query_info = SELECT * FROM `geo_city` WHERE `id` = $id # ForeignKey's sql_attr_uint = country_id } index common_city { source = geo_city path = /var/lib/sphinxsearch/data/geo_city docinfo = extern morphology = none stopwords = min_word_len = 2 charset_type = utf-8 min_prefix_len = 2 min_infix_len = 0 prefix_fields = name, alternatenames enable_star = 1 }
SPHINX_API_VERSION = 0x116
class Country(models.Model): name = models.CharField(max_length=200) code = models.CharField(max_length=10) population = models.IntegerField() latitude = models.FloatField() longitude = models.FloatField() alternatenames = models.CharField(max_length=2000, blank=True, default='') def __str__(self): return unicode(self.name).encode('utf-8') def __unicode__(self): return unicode(self.name) class City(models.Model): name = models.CharField(max_length=200) country = models.ForeignKey(Country) alternatenames = models.CharField(max_length=2000, blank=True, default='') latitude = models.FloatField(default=0) longitude = models.FloatField(default=0) adm = models.CharField(max_length=200) search = SphinxSearch(weights={ 'name': 100, 'alternatenames': 80 }) def __str__(self): return unicode(self.name).encode('utf-8') def __unicode__(self): return unicode(self.name)
class CustomUserProfile(models.Model): user = models.ForeignKey(User, unique=True) full_name = models.CharField(max_length=200, blank=True) city = models.ForeignKey(City, blank=True, null=True) country = models.ForeignKey(Country, blank=True, null=True, editable=False) date_registered = models.DateField(editable=False, auto_now_add=True)
class UserProfileForm(forms.ModelForm): ''' Form to edit profile''' full_name = forms.CharField(widget=forms.TextInput()) city = selectable.AutoCompleteSelectField( label='City please', lookup_class = common.lookups.CityLookup, required=False, ) def clean_city(self): """ Convert city code to city object """ city_id = int(self.data["city_1"].encode("utf8")) city = City.objects.get(id=city_id) return city class Meta: model = CustomUserProfile exclude = ("user",)
AUTH_PROFILE_MODULE = 'usermanage.CustomUserProfile'
(r'^selectable/', include('selectable.urls')),
class CityLookup(LookupBase): model = City item = None def get_query(self, request, term): qs = self.model.search.query(term + "*") return qs def get_queryset(self): return None def get_item_id(self, item): return item.id def get_item_value(self, item): if (not self.item): return smart_unicode(item) return smart_unicode(self.item.name) def get_item_label(self, item): return u"%s, %s" % (item.name, item.country) def get_item(self, value): item = None if value: try: item = City.objects.get(id=value) self.item = item except IndexError: pass return item try: registry.register(CityLookup) except: pass
import selectable.forms as selectable import geo.lookups</code>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script> <script type="text/javascript" src="/js/jquery/jquery-ui.min.js"></script> <script type="text/javascript" src="/js/jquery/jquery.dj.selectable.js"></script>
Source: https://habr.com/ru/post/128709/
All Articles