Daniel Quinn
Daniel Quinn

Reputation: 6418

Can Django have a ManyToMany relationship with global_settings.LANGUAGES?

I need my users to be able to state the langue(s) they speak, so naturally I started with:

class Language(models.Model):
    name = models.CharField(max_length=128)

class UserProfile(models.Model):
    languages = models.ManyToManyField("Language", related_name="users")

But then I discovered this:

from django.conf.global_settings import LANGUAGES

Which I'd like to use, if only to keep to the DRY principle. The problem is, I can't figure out how to allow a user to have multiple languages.

Upvotes: 1

Views: 269

Answers (1)

cha0site
cha0site

Reputation: 10737

How about...

class LanguageSpoken(models.Model):
     user = models.ForeignKey("UserProfile")
     language = models.CharField(max_length = 2, choices = LANGUAGES)

I'm assuming you don't need to keep any other data about the language (like the countries it is spoken in), you just want users to list the languages they speak. If you do need to keep other data, you'll have to use a many-to-many relationship.

Upvotes: 4

Related Questions