Django creating multiple objects in same model

395 views
Skip to first unread message

Syndicate kun

unread,
Jul 22, 2018, 10:39:54 PM7/22/18
to Django users
Hi guys i have a question
So i have a model called curriculainfo
   
class CurriculaInfo(models.Model):
        curricula_info_id
= models.AutoField(primary_key=True)
        curricula
= models.ForeignKey(Curricula,
                                      on_delete
=models.CASCADE)
        classes
= models.ForeignKey(Classes,
                                    on_delete
=models.CASCADE)
        designation
= models.ForeignKey(Designations,
                                        on_delete
=models.SET_NULL,
                                       
null=True,
                                        blank
=True,
                                        related_name
="designation")
   
       
class Meta:
            db_table
= "arc_curricula_info"
            verbose_name_plural
= "Curricula Information"
            unique_together
= ('curricula', 'classes', 'designation')
            ordering
= ['curricula_info_id']
   
       
def __str__(self):
           
return 'Course: %s, Module: %s, Class: %s, Instructor: %s (AY: %s, Sem: %s)' % (
               
self.curricula.course_period.course.course_abbreviation,
               
self.curricula.module_period.module.module_abbreviation,
               
self.classes.class_name,
               
self.designation.staff.staff_full_name,
               
self.curricula.course_period.period.academic_year,
               
self.curricula.course_period.period.semester)`enter code here`

heres my forms.py

 
   class AssignClassForm(forms.ModelForm):
        designation
= forms.ModelChoiceField(queryset=Designations.objects.filter(role__role_name='Instructor'),
                                             required
=False)
        curricula
= forms.ModelChoiceField(queryset=Curricula.objects.all(), widget=forms.HiddenInput)
   
       
class Meta(object):
            model
= CurriculaInfo
            fields
= ['curricula', 'classes', 'designation']


heres
my views.py


   
class CurriculumClasses(CreateView):
        model
= CurriculaInfo
        form_class
= AssignClassForm
        template_name
= 'architect/assignclasses.html'
        success_url
= '/create-success/'
   
       
def get_initial(self):
            course
= self.kwargs.get('course_abbr')
            year
= self.kwargs.get('year')
            semester
= self.kwargs.get('semester')
           
module = self.kwargs.get('module_abbr')
            curricula
= get_object_or_404(Curricula,
                                          course_period__course__course_abbreviation
=course,
                                          module_period__module__module_abbreviation
=module,
                                          course_period__period__academic_year
=year,
                                          course_period__period__semester
=semester)
           
return {
               
'curricula': curricula
           
}
   
       
def get_context_data(self, **kwargs):
            context
= super().get_context_data(**kwargs)
            context
['classes'] = Classes.objects.all()
           
return context

The get_initial method takes in values from the url to return the value curricula to the forms.py field, while the get_context_data sends classes values to the template 
  
   
 <form method="post" action="{% url 'curriculum_classes' course_abbr module_abbr year semester  %}">
          {% csrf_token %}
          {{ form.non_field_errors }}
          {{ form.curricula }}
         
<div class="fieldWrapper">
          {% for class in classes %}
         
<input class="form-check-input" type="checkbox" id="classes" value="{{ class.class_id }}">
         
<label class="form-check-label" for="autoSizingCheck">{{ class.class_name }}</label>
          {{ form.designation }}
                 
          {% endfor %}
         
</div>
           
<input class='btn btn-primary' type='submit' value='Submit'/>
           
</form>


In the html i use a loop to render checkboxes based on the records collected from the classes in views.py get_context_data. So in my use case i am trying get it so when i submit the form the number of object it creates is based on the number of checkboxes(representing classes) ticked. Then in each object the classes value will be different while the designation value will be based on dropdown select and curricula values for each of the object remains the same, my current method does not input any value so how to i go about doing this. 
Reply all
Reply to author
Forward
0 new messages