views:

37

answers:

1

I am a bit stuck on how I should model this out.

Here is what I have:

I have a model called Location. In this model I have postal code, city, region, longitude, and latitude. This data is pre-populated with all of Canada's stuff. You can imagine this table is quite large.

This is what I would like to achieve by stuck on how to model this:

I would like to create a second model called Item. Each one of these items will need to be tied to a location from the said above model. The user-story would be as follows:

User adds an item: I already know their postal code and city based on their cookie that I set.
User submits the form with their item: this is where I am confused as to how to model this data so that the item gets saved in the proper location.

I figured a FK would be the way to go but that is waaaaay to inefficient for a number of obvious reasons (huge list, and requires user input but I already know their location before saving). So, since I already know their location based on their cookie, should create a new field in the Item model called location and just save the postal code in this model? If I did this I guess I would have to query the location model for that location to pull in proper info. I am not sure what the best to go about this is, please help.

A: 

If you already know the user's location, and they're just entering an item, then the Item model should have a foreign key to Location, but you don't prompt for it on the form. Instead, fill in the Location before you save the item.

If you're using a ModelForm, then you'll want to exclude your location field so that it isn't displayed. You'll also want to set commit=False so that you can fill in the location yourself before saving the form data to the Item table.

Craig Trader
thanks craig, that's what I needed.
Jeffrey