2017-08-08 7 views
1

私は2つのモデルを使用しています。最初のものは一人のユーザーが複数の教育資格インスタンスを入力できる教育モデルです:django:UserinfoのインスタンスごとにEducationモデルのインスタンスが複数ある場合、モデルUserinfoの複数のインスタンスを取得するには

class Education(models.Model): 
    user = models.ForeignKey(User,on_delete=models.CASCADE) 
    degree_name = models.CharField(max_length=150,null=True,blank=True) 
    institute_name = models.CharField(max_length=150, null=True, blank=True) 
    date_start = models.CharField(null=True,blank=True,max_length=25) 
    date_end = models.CharField(null=True,blank=True,max_length=25) 
    description = models.TextField(null=True,blank=True,max_length=1000) 

第2のモデルが一人のユーザが最大のインスタンスを持つことが可能な「ユーザー情報」モデルである:

class Userinfo(models.Model): 
    user = models.ForeignKey(User,on_delete=models.CASCADE) 
    user_info = models.ForeignKey(User_info,related_name='user_info',on_delete=models.CASCADE,null=True) 

    profile_pic = models.FileField(null=True,blank=True) 
    dob = models.CharField(max_length=25,null=True,blank=True) 
    nationality = models.CharField(max_length=100, null=True, blank=True) 
    headline = models.CharField(max_length=160, null=True,blank=True) 
    summary = models.TextField(max_length=1000, null=True, blank=True) 
    current_salary = models.FloatField(null=True,blank=True) 
    japanese_level = models.CharField(max_length=50, null=True, blank=True) 
    english_level = models.CharField(max_length=50, null=True, blank=True) 
    career_level = models.CharField(max_length=50,null=True,blank=True) 
    availability = models.CharField(max_length=50, null=True, blank=True) 
    expected_salary = models.FloatField(null=True, blank=True) 
    job_role = models.CharField(max_length=50,null=True) 

のUserInfoをループするとき、私は複数のインを得ることができる必要がありますように、私は両方のモデルに関連することができますどのように
Userinfo.objects.filter(user=request.user) 

:私のような「ユーザー情報」のいずれかのインスタンスを取得するために、任意のクエリを使用する場合それは教育モデルの中にある。モデルを変更してクエリを実行するにはどうすればよいですか?

答えて

1

Educationモデル内のUserモデルには、すでに外部キーがあります。 UserInfoモデルに外部キーは必要ありません。あなただけの余分な呼び出しを行うことにより、特定のユーザーのすべてのEducationのインスタンスを取得することができます

Education.objects.filter(user=request.user) 

またはあなたが取得する必要があり、実際のユーザーにrequest.userを変更することができます。

EDIT:テンプレートに

def myView(request): 
    user_info = Userinfo.objects.get(user=request.user) #using get since only 1 instance always 
    educations = Education.objects.filter(user=request.user) #fetching all the instances for the education 

    context_dict = {"user_info": user_info} 
    educations_list = [] 



    for e in educations: 
     educations_list.append(e) 
     # do whatever you need with the educations 
     # you can access user_info fields just by `user_info.field_name` 
     # and you can access the current education fields by `e.field_name` 
    context_dict["educations"] = educations_list 

    return render(request, "template.html", context_dict) 

使用例のviews.py

例:あなたのコードに変更を加えることなく、

は、次のように複数のインスタンスを取得することができます.html

{% if user_info %} 
    <p>{{ user_info.field_name }}</p> 

    {% if educations %} 
     {% for e in educations %} 
      <div>{{ e.field_name }}</div> 
     {% endfor %} 
    {% endif %} 
{% endif %} 

(複数のユーザー情報のインスタンスを含む)EDIT 2

views.py

def myView(request): 
    user_infos = Userinfo.objects.filter() # fetch all instances 
    context_dict = {} 

    result = [] 

    for u in user_infos: 
     temp = [] 
     educations_list = [] 
     educations = Education.objects.filter(user=u.user) # fetch educations for the currently iterated user from user_infos 
     for e in educations: 
      educations_list.append(e) 
     temp.append(u) # append the current user_info 
     temp.append(educations_list) # append the corresponding educations 
     result.append(temp) 
    context_dict["result"] = result 
    return render(request, "template.html", context) 

template.html

{% if result %} 
    {% for r in result %} 
     <div>{{ r.0 }}</div> <!-- r.0 is your currently iterated user_info can be used like: r.0.profile_pic for example --> 
     {% if r.1 %} 
      {% for e in r.1 %} 
       <div>e.degree_name</div> <!-- e is the current education on the current user_info --> 
      {% endfor %} 
     {% endif %} 
    {% endfor %} 
{% endif %} 

はviews.pyのコードは完璧ではないとする価値があるかもしれませんリファクタリングを少し(最終的な辞書を構築する方法)、しかし、これはあなたにそれを行う方法のアイデアを与えると思います。

希望すると便利です。

+0

これは実際には私が探しているものではない、私は1つのクエリでユーザーのすべての教育インスタンスと一緒にUserInfoインスタンスを取得するために探しています。 – jencko

+0

私のプロジェクトでは、私は自分の教育と一緒にすべてのユーザー情報を1つのクエリで必要とする状況があるので、forループを使用して、それぞれのユーザーの情報をそれぞれの教育と共に印刷できます。 – jencko

+1

@jencko申し訳ありませんが、1つのクエリでこれを達成することはできませんが、このアプローチをご希望の場合は、私のやり方を説明することができます。 –

0
ui = Userinfo.objects.filter(user=request.user) 

このクエリはrequest.userのためにあなたにUserinfoのすべてのインスタンスを提供します。あなたは、このようにループでEducation属性の値にアクセスすることができます

for u in ui: 
    ui.education.degree_name 
    # and so on for other fields. 
0

私は多分あなたのUserInfoモデルがユーザーにOneToOneのrelationsshipを持っているし、次に希望このことができます

UserInfo.objects.filter(user=request.user).education_set.all() 

ような何かを行うことができると思います。

幸運を祈る!

関連する問題