views:

43

answers:

3

I want the ability to let users indicate what countries they have visited.. my models.py looks something like this:

class User(models.Model):
      name = models.CharField(max_length=50)
      countries = models.ManyToManyField(Countries)

class Countries(models.Model):
      #This is where I don't know what to do.
      #The end goal is for the user to be able to check off what countries he/she has visited
A: 

Relation fields already generate an attribute on the other model for the reverse relation unless explicitly disabled.

Ignacio Vazquez-Abrams
A: 

You're fine as is w/r/t the ManyToManyField.

Now you'll want to create a form for this model to allow the checking-off to be done by your users.

fish2000
Thanks for the info; just to clarify, so a simple HTML post will take care of this?
And there is nothing I need to write under the Countries class?
well -- to make the class compile, you'll need at least `pass` in countries, and probably more, for whatever country-specific logic you need.
fish2000
and by 'form', to get your HTML you'll want a django model form: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/ -- have a look there to get started.
fish2000
+1  A: 

You would create the relationship the other way around

 class User(models.Model):
     name = models.CharField(max_length=50)

 class Countries(models.Model):
     user = models.ForeignKey(User)

If you are using django's built in User stuff then you only need this.

from django.contrib.auth.models import User

class Countries(models.Model):
    user = models.ForeignKey(User)
Dronestudios
OK Thanks. I don't know anything about Django's User stuff, but will look at it. (first day on Django..)
That's only correct if you want the users to write down the countries and add a 'USA' for every user who visited the USA.
Till Backhaus