On Fri, Dec 5, 2008 at 3:38 AM, Margie <[EMAIL PROTECTED]> wrote:

>
> Am trying to get the hang of specifying my models, so bear with me ...
>
> Suppose that I want to reprsent states and the cities that are in
> them.  I think I would have a model that looks like this:
>
> class State(models.Model):
>    name = models.CharField('name', max_length=20)
>
> class City(models.Model):
>    name = models.CharField('name', max_length=20)
>    state = models.ForeignKey(State)
>
> Now, suppose that cities can have the same name, as long as they are
> in different states, and I want a way to check that when the user adds
> a city, that he/she doesn't add it twice for the same state.  IE, the
> user has data for certain states and cities and wants to add only
> those for which there is data.  The user will manually click on "add
> state", which will take them to a page where they input some state
> data and then can click a 'submit' button to save it.  And also from
> that window they can click on an 'add city' button that will take them
> to a window where they can input data for a city and then submit
> that.  If I want to make sure that the same city isn't added twice for
> the same state, do I simply do that check when the city data is
> "posted"?  IE, if they try to add the same city twice, I just give an
> error saying it's already there?
>

Specify unique_together for name and state in your City model.  See:

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together


> What if I want the user to be able to enter a whole bunch of cities at
> once, for example by entering them all in a table on a single page.
> What would be the appropriate way to represent that table?
>

Formsets or model formsets could be the way to go here:

http://docs.djangoproject.com/en/dev/topics/forms/formsets/
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to