django:当Userinfo的每个实例在Education模型中有多个实例时,如何获得模型Userinfo的多个实例

时间:2017-08-08 08:13:04

标签: django django-models

我有两个与我合作的模特。第一个是教育模式,其中一个用户可以输入多个教育资格实例:

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)

我如何关联这两个模型,以便在循环访问Userinfo时,我应该能够在Education模型中获得它的多个实例。我该如何更改模型并查询它们?

3 个答案:

答案 0 :(得分: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)

template.html中的示例用法

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

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

编辑2(包括多个userinfo实例)

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中的代码并不完美,可能值得重构(如何构建最终的字典),但我相信这会让你知道如何去做。

希望这有帮助!

答案 1 :(得分:0)

UITabBarController

此查询将为ui = Userinfo.objects.filter(user=request.user) 提供Userinfo的所有实例。你可以像这样循环访问request.user属性的值:

Education

答案 2 :(得分:0)

我想也许您的UserInfo模型可以与用户建立OneToOne关系,然后执行类似

的操作
UserInfo.objects.filter(user=request.user).education_set.all()

希望这有帮助。

祝你好运!