Form control in Django ForeignKey

给你一囗甜甜゛ 提交于 2019-12-11 06:14:10

问题


My Models:

class Faculty(models.Model):
    name = models.CharField(max_length=30)


class Program(models.Model):
    name = models.CharField(max_length=30)
    faculty = models.ForeignKey(Faculty)


class Student(models.Model):
    name = models.CharField(max_length=30)
    faculty = models.ForeignKey(Faculty)
    program = models.ForeignKey(Program)

Let I have Two Faculty:

  • Science
  • Management

And I have 3 Programs in each:

  • Science
    • B. Computer
    • B. Software
    • B. Civil
  • Management
    • BBA
    • BBS
    • BBI

And What I want is When A studen Is Filling up a Form They can Select Faculty and Programs. So When a user select Science as Faculty. Then How to make django Gives Only Programs From That selected Faculty? That means when users select Science in Faculty Field Then in program field Computer, Software and Civil should be Shown.

Is it possible? then how? I think I make a clear question. (easy to understand what I mean)

Update:- as asked By @thameem

forms.py

class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = {'name','faculty','program','roll','gender','contact','address','about',}

Template:

<div class="contener">
<form method="post" enctype="multipart/form-data">{% csrf_token %}
            {{form.as_p}}

            <input type="submit" value="Save">
</form>
</div>

urls.py

urlpatterns = [
url(r'^addprofile/$', addprofile, name='addprofile'),
]

回答1:


you can done it by using ajax

put this code in your template

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>

    $(document).ready(function(){

        $("select[name='faculty']").change(function(){
            var value =  $(this).val();
            var pselect = $("select[name='program']");

            $.ajax({
                method:"get",
                dataType:"json",
                url:'{% url 'profile:suggest_program' %}',
                data:{
                'faculty':value
                },
                success:function(data){
                    pselect.empty();
                    $.each(data, function(index, value){
                        pselect.append("<option value='"+value.pk+"'>"+value.name+"</option>");
                    });
                },

            });
        });

    });
    </script>

in your views.py

from django.http import JsonResponse


def suggest_program(request):
    faculty = request.GET.get("faculty")
    programs = [{"data":"nothing found"}]
    if faculty:
        programs = Program.objects.filter(faculty_id=faculty
                                            ).values("pk", "name")
        programs = list(programs)
    return JsonResponse(programs, safe=False)

urls.py

url(r'^suggest-program/$', views.suggest_program, name="suggest_program"),


来源:https://stackoverflow.com/questions/43478355/form-control-in-django-foreignkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!