限制基于其他领域的外国领域的选择
Limited the choices of a foreign field based on other field
型号
from django.db import models
from test.test_imageop import MAX_LEN
class Student(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
mail_adress = models.EmailField(max_length=128)
def __unicode__(self):
return self.first_name+" "+self.last_name
class Course(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
students=models.ManyToManyField(Student)
def __unicode__(self):
return self.name
# Create your models here.
class Task(models.Model):
course= models.ManyToManyField(Course)
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
students=models.ManyToManyField(Student,limit_choices_to={'course' : Course},blank=True)
def __unicode__(self):
return self.name
管理员
from django.contrib import admin
from .models import Student
from .models import Course
from .models import Task
class StudentAdmin(admin.ModelAdmin):
pass
admin.site.register(Student,StudentAdmin)
class CourseAdmin(admin.ModelAdmin):
pass
admin.site.register(Course,CourseAdmin)
class TaskAdmin(admin.ModelAdmin):
pass
admin.site.register(Task,TaskAdmin)
# Register your models here.
我的想法是,当我创建一个任务并 select 一门课程时,只有分配给这门课程的学生可以 selected 并且给定这个任务我尝试了很多答案对于其他成员,但在这种情况下不起作用,如果您有想法,请提供帮助。
您需要在表单中过滤学生。在管理员中,这个 hack 可能会有所帮助:
class TaskAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
request._obj_ = obj
return super(TaskAdmin, self).get_form(request, obj, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
field = super(TaskAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'students':
if request._obj_ is not None:
field.queryset = field.queryset.filter(id__in=request._obj_.course.students.all())
else:
field.queryset = field.queryset.none()
return field
为了你的ModelForm
:
class TaskForm(forms.ModelForm):
def __init__(self, course, *args, **kwargs):
super(TaskForm, self).__init__(*args, **kwargs)
self.fields['students'].queryset = course.students
class Meta:
model = Task
fields = '__all__'
然后你必须在实例化表单时通过课程:
form = TaskForm(course, request.POST)
您需要在 admin.py 中使用 formfield_for_manytomany
文档在这里
https://docs.djangoproject.com/en/1.7/ref/contrib/admi/#django.contrib.admin.ModelAdmin.formfield_for_manytomany
型号
from django.db import models
from test.test_imageop import MAX_LEN
class Student(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
mail_adress = models.EmailField(max_length=128)
def __unicode__(self):
return self.first_name+" "+self.last_name
class Course(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
students=models.ManyToManyField(Student)
def __unicode__(self):
return self.name
# Create your models here.
class Task(models.Model):
course= models.ManyToManyField(Course)
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
students=models.ManyToManyField(Student,limit_choices_to={'course' : Course},blank=True)
def __unicode__(self):
return self.name
管理员
from django.contrib import admin
from .models import Student
from .models import Course
from .models import Task
class StudentAdmin(admin.ModelAdmin):
pass
admin.site.register(Student,StudentAdmin)
class CourseAdmin(admin.ModelAdmin):
pass
admin.site.register(Course,CourseAdmin)
class TaskAdmin(admin.ModelAdmin):
pass
admin.site.register(Task,TaskAdmin)
# Register your models here.
我的想法是,当我创建一个任务并 select 一门课程时,只有分配给这门课程的学生可以 selected 并且给定这个任务我尝试了很多答案对于其他成员,但在这种情况下不起作用,如果您有想法,请提供帮助。
您需要在表单中过滤学生。在管理员中,这个 hack 可能会有所帮助:
class TaskAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
request._obj_ = obj
return super(TaskAdmin, self).get_form(request, obj, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
field = super(TaskAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'students':
if request._obj_ is not None:
field.queryset = field.queryset.filter(id__in=request._obj_.course.students.all())
else:
field.queryset = field.queryset.none()
return field
为了你的ModelForm
:
class TaskForm(forms.ModelForm):
def __init__(self, course, *args, **kwargs):
super(TaskForm, self).__init__(*args, **kwargs)
self.fields['students'].queryset = course.students
class Meta:
model = Task
fields = '__all__'
然后你必须在实例化表单时通过课程:
form = TaskForm(course, request.POST)
您需要在 admin.py 中使用 formfield_for_manytomany
文档在这里
https://docs.djangoproject.com/en/1.7/ref/contrib/admi/#django.contrib.admin.ModelAdmin.formfield_for_manytomany