Collection inside another collection


#1

Hi there,

I have created a collection of US states and would like to create a collection of cities inside each state. What would be the best approach to implement this?

Thank you and sorry if this was already asked before!!


#2

Neat idea! There are a few different ways to approach this… do you think the data will be updated?

Like would you want a way to add or remove cities (so, adding your data to a database)?

Or will your data stay static and not be updated (so you’d set it in your code, which could be accessed by other parts of your app, but not updated/changed through your website)?

Let me know if I can explain further. :)


#3

Yes! The data would be updated. I would like to have the ability to add and remove cities in every state.


#4

Okay cool, got it.

I am assuming states would be static — sounds like making a model for your cities and then use a field to hold your “state” for that city, which could be a CharField that has choices set, for example:

city = models.CharField(max_length=255, choices=STATE_CHOICES)

(And STATE_CHOICES would be a list of all states.)

Then the rest of Hello Web App’s tutorial (are you using it?) would apply — you can use the admin to add/remove cities, or build a form for your app to edit/add.

I will mention things will get hairy if you’re looking to add geolocation or search by log/lat.

Let me know if I help further!


#5

This is just what I needed!
Thank you for all your help, I really enjoyed the tutorial and it was very helpful and fun!