2016-03-26 15 views
0

ユーザーを認証するためにdjango-rest-authを使用しているdjangoプロジェクトがあります。私のsettings.py:運動評価ユーザーオブジェクトを使用したDjango-rest-auth

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', 
) 

私は2つのモデルを持っています。 ユーザモデルは、django-rest-auth/django.contrib.authで提供する必要があるため作成しませんでした。 ユーザーは、特定の運動に複数の評価を与えることができます。

私のmodels.pyには、以下のようになります。

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) 

評価モデルはまた、右、exerciseidとともに外部キーとしてユーザーIDが含まれている必要があり、上記の説明によると?

どうすればこの問題を解決できますか?評価モデルを変更するにはどうすればよいですか?私は何をインポートする必要がありますか?

答えて

0
from django.contrib.auth.models import User 

class Rating(models.Model): 
    user = models.ForeignKey(User) 
    # other fields 

私はこれがあなたが探しているものだと思います。 Here私はあなたの質問に関連すると思う例です

+0

しかし、oneTooneFieldではなくForeign Keyであるべきですか? – Nitish

+0

申し訳ありません。私の悪い。私はあなたの質問を非常に素早く読んで、自分のモデルにユーザーに対応するフィールドを追加する方法を質問しました。私は答えを修正します。 – voger

関連する問題