问题描述:

Sorry for bad English and less information

# 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

A lecturer can be assigned as many as course.I want to return a queryset with courses assigned for a particular username.

网友答案:

return qs.filter( ????? ) # particular courses for a username

You can do

return request.user.lecturer_set.get().course_set.all()
相关阅读:
Top