Thursday, September 27, 2018

how to filter foreign key in django form without passing foreign key value?

I have created two models in my django project AddStudent and Fee Entry as shown below.

models.py

class AddStudent(models.Model):      enrollment_no = models.BigIntegerField(primary_key=True)      student_name = models.CharField(max_length=500,null=True)      gender = models.CharField(max_length=1,choices=GENDER_CHOICES)      course = models.ForeignKey(CourseMaster, on_delete=models.DO_NOTHING, null=True)      category= models.ForeignKey(CatMaster, on_delete=models.DO_NOTHING, null=True)      admission_year = models.IntegerField(('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)      college = models.ForeignKey(CollegeMaster, on_delete=models.DO_NOTHING, null=True)      branch = models.ForeignKey(BranchMaster,on_delete=models.DO_NOTHING, null=True)      current_semester = models.IntegerField(null=True)      address = models.CharField(max_length=1000,null=True)      city = models.CharField(max_length=100,null=True)      district = models.CharField(max_length=100,null=True)      state = models.CharField(max_length=100,null=True)      student_contact = models.BigIntegerField()      parent_contact = models.BigIntegerField()        def get_absolute_url(self):          return reverse('add_student:index')        def __str__(self):          return str(self.enrollment_no) + ' - ' + self.student_name    class FeeEntry(models.Model):        student = models.ForeignKey(AddStudent,on_delete=models.DO_NOTHING)      fee_detail = models.ForeignKey(FeeMaster,on_delete=models.DO_NOTHING)      fee_sem = models.IntegerField(null=True)      payment_date = models.DateField(("Date"), default=datetime.date.today)      pay_method = models.BooleanField(choices=BOOL_CHOICES)      cheque_no = models.CharField(max_length = 100, null=True, blank=True)      bank_name = models.CharField(max_length = 200, null=True, blank=True)        def __str__(self):          return str(self.id) + ' - ' + str(self.student) + ' - ' + self.student.student_name

Now when user search particular student for example student id = 1 than student profile page will open and there is another button addfee. My problem is when user click on add fee all 500 student list is appear in dropdown list. i want to create fee for searched student only.

forms.py

from django import forms  from .models import FeeEntry, AddStudent  from bootstrap_modal_forms.mixins import PopRequestMixin, CreateUpdateAjaxMixin    class FeeForm(PopRequestMixin, CreateUpdateAjaxMixin, forms.ModelForm):      class Meta:          model = FeeEntry          fields = ['student', 'fee_detail', 'fee_sem', 'payment_date', 'pay_method','cheque_no','bank_name']

above is my forms.py file where field 'student' will generate all 500 student list. I want only selected student for example enrollment_no=1 when user is on enrollment_no 1's page.

views.py

class FeeCreateView(PassRequestMixin, SuccessMessageMixin,                           generic.CreateView):      template_name = 'add_student/create_fee.html'      form_class = FeeForm      success_message = 'Success: Book was created.'      success_url = reverse_lazy('add_student:detail')  

urls.py

path('create/<int:pk>', views.FeeCreateView.as_view(), name='create_fee'),  

Can anyone tell me what changes are required in this code? or can you share link of similar example like this?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c156951b-d125-4664-a12e-4306db0f43aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment