Saif Alradhi
Saif Alradhi

Reputation: 129

Limited the choices of a foreign field based on other field

MODELS

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

Admin

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.

The idea is that, when I create a task and I select a course, only the students assigned to this course can be selected and given this task I tried a lot of answers for another members but it doesn't work in this case, If u have an idea please help on it.


Upvotes: 0

Views: 245

Answers (2)

nima
nima

Reputation: 6733

You need to filter students in your forms. In admin this hack might help:

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

For your ModelForms:

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__'

You would then have to pass in the course when you instantiate the form:

form = TaskForm(course, request.POST) 

Upvotes: 1

Ymartin
Ymartin

Reputation: 1361

You will need to use formfield_for_manytomany in your admin.py The documentation is here https://docs.djangoproject.com/en/1.7/ref/contrib/admi/#django.contrib.admin.ModelAdmin.formfield_for_manytomany

Upvotes: 1

Related Questions