How do I allow a user to to select an arbitrary number of 'n' languages during account registration?

Viewed 20

Suppose I am creating the next language learning application like Duolingo. Initially during the account registration process, the user can select up to 5 studying languages. At a later time, a user can subscribe to a premium subscription and set up to N number of languages in a different page like in their account settings. These selected languages will eventual filter a user's feed.

How should I go about this in the proper way? What would something like this be called, so that I can read more about it in the documentation. Thank you kindly.

It would be nice if I could define the allowed number directly in the settings file:

STANDARD_USER_LANGUAGE_ALLOWANCE = 3
SUBSCRIBED_USER_LANGUAGE_ALLOWANCE = 5

Method 1 (Most likely terrible and not DRY):

class User(AbstractUser):
    // Manually list all of them here and keep adding in the future
    language1 = ...
    language2 = ... 
    language3 = ...

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

    short_name = models.CharField(
        max_length=20,
    )

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']

Method 2 (Maybe use a ManyToMany relationship with a multiple checkbox as the widget)? Would this even be a good application of ManyToMany?

class User(AbstractUser):
  languages = models.ManyToManyField(Language)
0 Answers
Related