5

I am still very new to Django and trying to figure out what is the best approach for the following problem.

I want to store user personal preferences (things user likes or dislikes) as simple boolean values - true or false. The thing is that preferences are not fixed and new preferences can be added via admin interface.

This is what I have in mind.

Preferences:

class Preferences(models.Model):
    title = models.CharField(max_length=100)
    ...

Values:

class PreferencesValues(models.Model):
    user = models.ForeignKey(User)
    preference = models.ForeignKey(Preferences)
    value = models.BooleanField()

Is this the way to go or is there some better approach? Also, what would be the best way to make a form that displays all available preferences with checkboxes, but sets initial values to checked for those preferences which user has already marked?

Kaspars B.
  • 362
  • 2
  • 10
  • There are more up to date answers on related questions: http://stackoverflow.com/questions/6085025/django-user-profile – ptim May 23 '16 at 23:53

2 Answers2

0

If all your preferences values are Boolean, then yes, that way could be acceptable. And if you want to handle the PreferencesValues with Django Forms, you should take a look at Django Formsets.

But if you want to make something ajax-able, you should also take a look at this Django Snippets article.

Nat Riddle
  • 928
  • 1
  • 10
  • 24
obayhan
  • 1,636
  • 18
  • 35
0

Can try using the package "django-userpreferences" https://pypi.org/project/django-userpreferences/ . I did not used it yet, but was researching on the similar requirement for capturing User Preferences, and got to know above link, hoping that will help others thus posted here. Will be using this soon to check how it works, and if it satisfy all my requirements.

Mayank Tripathi
  • 489
  • 5
  • 16