如何通过子代外键过滤Parents ID?

时间:2019-09-19 03:27:56

标签: django filter

观看次数

def Parents_login_form(request):
   if request.method != 'POST':
      raise Http404('Only POSTs are allowed')
   try:
        m = ParentsProfile.objects.get(Parent_User=request.POST['p_user'])
        if m.Parent_Password == request.POST['p_pass']:
            aa = request.POST['p_user']
            parents = ParentsProfile.objects.all().filter(Parent_User=aa)

            student = request.POST.get('parentsID')
            students = StudentProfile.objects.filter(Parent_Users = student)
            print(students)
            return render(request, 'accounts/ParentsProfile.html', {"parents": parents, "students": students})

   except ParentsProfile.DoesNotExist:
        messages.warning(request, 'Please correct the error below.')

   return render(request, 'accounts/Plogin_form.html')

html

{% for me in parents %}
 <input type="hidden" value="{{me.id}}" name="parentsID" id="parentsID" onchange="" readonly>
{% endfor %}

模型

class StudentProfile(models.Model):
    .
    .
    .
    Parent_Users = models.ForeignKey(ParentsProfile, related_name='+', on_delete=models.CASCADE,null=True,blank=True,)

1 个答案:

答案 0 :(得分:0)

尝试一下,y是父模型中的字段,x是子模型中的字段:

{% for y in parents %}
{% for x in y.+.all %}

<input type="hidden" value="{{y.id}}" name="parentsID" id="parentsID" onchange="" readonly>

{% endfor %}
{% endfor %}