yourlabs / django-cities-light

A simple app providing three models: Country, Region and City model. Also provided, a command to insert or update data from geonames database dumps. Status: stable.
http://django-cities-light.rtfd.org/
MIT License
335 stars 126 forks source link

Is there an easy way to get custom fields in the Country model? #16

Closed benkonrath closed 12 years ago

benkonrath commented 12 years ago

Great job on this app. It fills a big hole in easily getting geonames data into Django sites with postgis. The site I'm working on now has a requirement to have Countries ODA Recipients status and UN Subregion listed. Is there an easy way to add custom fields the Country model? Both of these values aren't in geonames so I can add that information myself after the geonames information has been imported.

I briefly tried to get something going by sub-classing the Country model and adding the fields I needed. That didn't work too well because the Country model was created and not the sub-classed model when using the cities_light management command. I could use a signal to create an additional model to hold the info I need when the Country model created (like User and UserProfile) but I'd still have an awkward admin interface with data in two models. Is there a better way I can accomplish this task or would it better to just fork the repo and add the extra fields I want?

Thanks, Ben

  1. http://www.oecd.org/dac/aidstatistics/daclistofodarecipients.htm
  2. https://en.wikipedia.org/wiki/Subregion#United_Nations_subregions
jpic commented 12 years ago

Would it work to make another model with a OneToOne or ForeignKey to Country ? That's the simplest solution I can think of, it's also how I use to attach data to django.contrib.auth.models.User and such.

Another solution to add fields to a model is to use contribute_to_class, ie.:

models.CharField(max_length=100, null=True, blank=True).contribute_to_class(Country, 'recipients_status')

That's the kind of stuff I stack in project_specific/models.py, where project_specific is an app where I put non-reusable stuff that is specific to a project. But you could also put that in something like project_cities_light/models.py, where project_cities_light would contain stuff that your project needs to add to cities_light.

Note that contribute_to_class might break in future versions of Django but it's been there for years so it's pretty unlikely... I've used it for a long time without any problem.

Thank you for your feedback !

jpic commented 12 years ago

Did you get satisfaction ?

Thanks, James