I have a django project in which I am using a django-rest-auth to authenticate users. My settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
#following was added to allow cross domain requests
'corsheaders',
#following were added for ssl thing
'djangosecure',
'sslserver',
#following were added for authentication
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'demo',
)
I have two models: Exercise and Rating. I did not create a User model as it must be provided by django-rest-auth / django.contrib.auth. A user can give multiple ratings to a particular exercise.
My models.py look like following:
class Exercise(models.Model):
#Field for storing exercise type
EXERCISE_TYPE_CHOICES = (
(1, 'Best stretch'),
(2, 'Butterfly reverse'),
(3, 'Squat row'),
(4, 'Plank'),
(5, 'Push up'),
(6, 'Side plank'),
(7, 'Squat'),
)
exercise_type = models.IntegerField(choices=EXERCISE_TYPE_CHOICES)
#Field for storing intensity level
INTENSITY_LEVEL_CHOICES = (
(1, 'Really simple'),
(2, 'Rather Simple'),
(3, 'Simple'),
(4, 'Okay'),
(5, 'Difficult'),
(6, 'Rather Difficult'),
(7, 'Really Difficult'),
)
intensity_level = models.IntegerField(choices=INTENSITY_LEVEL_CHOICES)
#Field for storing video url for a particular exercise
video_url = models.URLField()
#Field for storing description of the exercise
description = models.CharField(max_length=500)
class Rating(models.Model):
#Field for storing exercise type
exercise = models.ForeignKey(Exercise, related_name='ratings', blank=True, null=True)
#Field for storing rating
RATING_CHOICES = (
('H', 'Happy'),
('N', 'Neutral'),
('S', 'Sad'),
)
value = models.CharField(max_length=1,choices=RATING_CHOICES)
According to the description above my Rating model should also contain userid as a foreign key along with exerciseid, right?
How can I achieve this? How can I modify my Rating model? What I will need to import?
from django.contrib.auth.models import User
class Rating(models.Model):
user = models.ForeignKey(User)
# other fields
I think this is what you are looking for. Here is an example that I think is related to your question