2017-08-08 52 views
1

我有兩個與之合作的模型。第一個是教育模式,即一個用戶可以輸入多個學歷情況:django:當Userinfo的每個實例在教育模型中有多個實例時,如何獲取模型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) 

第二種模式是在其中一個用戶最多可以有一個實例的「用戶信息」模式:

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.objects.filter(user=request.user) 

我怎麼可以涉及這兩款車型,使得通過用戶信息循環的時候,我應該能夠得到多個插件它在教育模式中的作用。我應該如何改變我的模型並查詢它們?

回答

1

我看到您已在Education模型中有用戶模型的外鍵。在UserInfo模型中不需要外鍵。您只需通過進行額外調用獲取所有給定用戶的Education實例:

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

,或者你可以改變request.user到你需要得到實際的用戶。

編輯:

而不進行任何更改到你的代碼,你可以通過以下方式將多個實例:在模板

例如views.py

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) 

用法示例.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

實際上,這不是我正在尋找的,我正在尋找在一個查詢中獲取UserInfo實例以及用戶的所有教育實例。 – jencko

+0

我的項目中存在一種情況,我需要在一次查詢中將所有用戶信息與他的教育一起使用,以便我可以使用for循環並打印所有用戶的信息以及他們各自的教育。 – jencko

+1

@jencko我很抱歉,但我無法幫助您在1個查詢中實現該功能,但如果您希望採用這種方法,我可以爲您提供指導。 –

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

此查詢會給你的Userinfo所有實例爲request.user。你可以用循環這樣的訪問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() 

希望這有助於。

祝你好運!

相關問題