Monday, April 30, 2012

Re: Trouble with query syntax in Django view

First of all. Student.objects.filter(pk=id) returns QuerySet not
instance of Student.
If you want get current student use
get_object_or_404()(https://docs.djangoproject.com/en/1.4/topics/http/shortcuts/#get-object-or-404)
or Student.objects.get(pk=id).

Teacher and parents can bee retrived via Student instance:
teacher = student.teacher
parents = student.parents.all()

2012/5/1 LJ <ljayadams@gmail.com>:
> I have a Student model that stores information about a student,
> including the student's teacher.
> The teacher's information is stored in the Employee model that
> inherits from the Person Model.
> I am having trouble figuring out how to query the teacher, when a
> student id is passed as an ajax argument to the view function.
> I have no trouble returning the data about the parents and the
> student, but my filter for querying the teacher info is incorrect.
> Does anyone have any ideas how I can query for the teacher information
> based on the models below?
>
> #models.py
> class Student(Person):
>    grade = models.ForeignKey('schools.Grade')
>    school = models.ManyToManyField('schools.School', blank=True,
> null=True, related_name="school")
>    parents = models.ManyToManyField('parents.Parent', blank=True,
> null=True)
>    teacher = models.ForeignKey(Employee, blank=True, null=True,
> related_name='teacher')
>
> class Employee(Person):
>    ''' Employee model inherit Person Model'''
>    role=models.ForeignKey(EmployeeType, blank=True, null=True)
>    user=models.ForeignKey(User)
>    schools =
> models.ManyToManyField(School,blank=True,null=True)
>
> # views.py
> def get_required_meeting_participants(request, template):
>    try:
>        id=request.GET['id']
>    except:
>        id=None
>
>    parents = Parent.objects.filter(student=id)
>    student = Student.objects.filter(pk=id)
>    teacher = Employee.objects.filter(pk=student.teacher_id)  #this
> line is incorrect...help, please?
>
>    data = {
>        'parents': parents,
>        'student': student,
>        'teacher': teacher,
>    }
>
>    return
> render_to_response(template,data,
>                            context_instance=RequestContext(request))
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment