2011-02-04 72 views
1

對不起,我英文不好,少信息如何過濾django「admin.py」中ManyToManyField表的結果?

# Models.py # 
class Course(models.Model): 
    course_code = models.CharField(max_length=100) 
    title = models.CharField(max_length=200) 
    short = models.CharField(max_length=50) 
    elective_group = models.CharField(max_length=100) 
    def __unicode__(self): 
     return self.course_code 

class Lecturer(models.Model): 
    username = models.ForeignKey(User) 
    assigned_course = models.ManyToManyField(Course) 

#admin.py# 
from django.contrib import admin 
from mysite.question_bank.models import * 


class CourseAdmin(admin.ModelAdmin): 
    list_display = ('course_code', 'title', 'short') 
    def queryset(self, request): 
     qs = super(CourseAdmin, self).queryset(request) 
     if request.user.is_superuser: 
      return qs #(all the courses) 
     else: 
      return qs.filter(?????) # particular courses for a username 

講師可以分配多達course.I希望與分配給特定的用戶名課程返回的查詢集。用戶名

回答

0

return qs.filter(?????)#特定課程

你可以做

return request.user.lecturer_set.get().course_set.all() 
+0

u能請給你回答這個問題的簡要說明。 – anks 2011-02-04 12:47:33

相關問題