How do create a custom save method for an existing m2m field. (Django)How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How do I do a not equal in Django queryset filtering?django - inlineformset_factory with more than one ForeignKey'NoneType' object is not subscriptable in using django smart selectsDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Customer User Authentication error : AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user_management.CustomUser'How to set dynamic initial values to django modelform fieldHow to implement update_or_create inside create method of ModelSerializer

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

How do I check if a string is entirely made of the same substring?

Negative Resistance

How long after the last departure shall the airport stay open for an emergency return?

What is the best way to deal with NPC-NPC combat?

Does a large simulator bay have standard public address announcements?

Co-worker works way more than he should

Combinatorics problem, right solution?

Contradiction proof for inequality of P and NP?

How to not starve gigantic beasts

How bug prioritization works in agile projects vs non agile

How to pronounce 'c++' in Spanish

Unknown code in script

How can I practically buy stocks?

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

What does "function" actually mean in music?

Why do games have consumables?

Drawing a german abacus as in the books of Adam Ries

How do I reattach a shelf to the wall when it ripped out of the wall?

Is there metaphorical meaning of "aus der Haft entlassen"?

How to have a sharp product image?

Should the Product Owner dictate what info the UI needs to display?

Why must Chinese maps be obfuscated?



How do create a custom save method for an existing m2m field. (Django)


How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How do I do a not equal in Django queryset filtering?django - inlineformset_factory with more than one ForeignKey'NoneType' object is not subscriptable in using django smart selectsDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Customer User Authentication error : AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user_management.CustomUser'How to set dynamic initial values to django modelform fieldHow to implement update_or_create inside create method of ModelSerializer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















  • I have a profile model which contains education and created a many to
    many relation i.e many profiles can have many educations.

  • I have a template called Profile settings which contains multiple forms like Education CreateView, Education UpdateView and Education ListView. I'm using bootstrap modals to display these forms. Hence the they share same template.

  • Problem is whenever I save education form, it saves form in database
    and it shows in the education multiselect field of that profile in
    django admin but unselected, it does not create a m2m link between
    profile and education.

  • I overrided the save() method to create the link as suggested by the community but I'm not able to get the current profile. Getting this error after saving. ('EducationForm' object has no attribute 'request')

Models



class Profile(models.Model):
phone = models.CharField(max_length=11, null=True, blank=True)
education = models.ManyToManyField(Education, null=True, blank=True, related_name="education")
full_name = models.CharField(max_length=30, null=True, blank=True)

class Education(models.Model):
degree = models.CharField(max_length=100, null=True, blank=True)
school = models.CharField(max_length=100, null=True, blank=True)
edu_start_date = models.DateField(null=True, blank=True)
edu_end_date = models.DateField(null=True, blank=True)


View



class EducationView(CreateView):
model = Education
form_class = EducationForm
pk_url_kwarg = 'pk'
template_name = "profile_settings.html"

class ProfileSettingsView(UpdateView):
model = Profile
form_class = ProfileSettingsForm
pk_url_kwarg = 'pk'
context_object_name = 'object'
template_name = 'profile_settings.html'

def get_success_url(self):
return reverse_lazy('users:profile_settings', args = (self.object.id,))

def get_object(self):
pk = self.kwargs.get('pk')
return get_object_or_404(Profile, id=pk)

def get_context_data(self, **kwargs):
context = super(ProfileSettingsView, self).get_context_data(**kwargs)
context['prof'] = self.get_object()
return context


Form



class EducationForm(forms.ModelForm):

degree = forms.CharField(max_length=40, required=False)
school = forms.CharField(max_length=40, required=False)
edu_start_date = forms.DateField(required=False,
input_formats=settings.DATE_INPUT_FORMATS,
widget=forms.DateInput(attrs='readonly': 'readonly'))
edu_end_date = forms.DateField(required=False,
input_formats=settings.DATE_INPUT_FORMATS,
widget=forms.DateInput(attrs='readonly': 'readonly'))

def save(self, commit=True):
edu = super(EducationForm, self).save(commit=False)
edu.save()
profile = Profile.objects.get(self.request.user.profile.id)
profile.education.add(education=edu)

return edu


class Meta:
model = Education

fields = ['degree','school','edu_start_date','edu_end_date']

class ProfileSettingsForm(forms.ModelForm):
class Meta:
model = Profile

fields = ['phone','full_name','education']

widgets =

'full_name': forms.TextInput('class': 'form-control'),
'phone': forms.TextInput('class': 'form-control'),



urls



urlpatterns = [
path('<int:pk>/education/update/', views.EducationUpdate.as_view(), name='education_update'),
path('<int:pk>/profile/settings', views.ProfileSettingsView.as_view(), name='profile_settings'),
]


Template (Profile_settings.html)



<form action="#" style="padding-left: 15px; padding-right:15px;" method="POST">
form.errors
% csrf_token %
<div class="row">
<div id="education_update" class="modal fade" role="dialog">
<div class="modal-dialog">

<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">New Education</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<div class="field">
<label>Degree title<span class="red-txt">*</span></label>
<!-- <input class="form-control" type="text" name="degree" value="" maxlength="60" size="50"> -->
edu_object.degree
</div>
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
<div class="field">
<label>School<span class="red-txt">*</span></label>
<!-- <input class="form-control" type="text" name="school" value="" maxlength="60" size="50"> -->
edu_object.school
</div>
</div>
</div>

<div class="col-lg-6">
<div class="form-group">
<div class="field">
<label>Start date</label>
<div id="datepicker1" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
edu_object.edu_start_date
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
</div>

<div class="col-lg-6">
<div class="form-group">
<div class="field">
<label>End date</label>
<div id="datepicker2" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
edu_object.edu_end_date
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
</div>

<div class="col-lg-12">
<br>

<div style="text-align:center;" class="login login-button">
<input type="submit" class="btn btn-outline-primary btn-lg" style="cursor: pointer;" value="Save">
</div>
</div>
</div>
</div>
</div>

</div>
</div>
</div>
</form>


% if prof %
<table class="table">
<tbody>
% for edu in prof.education.all %
<tr class="divbutton" style="height: 90px;">
<td>
<div class="row">
<div style="padding-left: 40px; font-size: 20px;"> edu.degree </div>
<div style="padding-left: 40px; font-size: 20px;"> edu.school </div>
</div>
</td>
<td class="align-middle">
<div class="row">
<div id="button_under" style="margin-right: 20px;" class="login login-button">
<a href="#" class="btn btn-info" data-toggle="modal" data-target="#education_update" data-backdrop="false"><i class="fa fa-edit"></i> Edit</a>
</div>
<div id="button_under" class="login login-button">
<a href="" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</a>
</div>
</div>
</td>
</tr>
% endfor %
% endif %
</tbody>
</table>









share|improve this question




























    0















    • I have a profile model which contains education and created a many to
      many relation i.e many profiles can have many educations.

    • I have a template called Profile settings which contains multiple forms like Education CreateView, Education UpdateView and Education ListView. I'm using bootstrap modals to display these forms. Hence the they share same template.

    • Problem is whenever I save education form, it saves form in database
      and it shows in the education multiselect field of that profile in
      django admin but unselected, it does not create a m2m link between
      profile and education.

    • I overrided the save() method to create the link as suggested by the community but I'm not able to get the current profile. Getting this error after saving. ('EducationForm' object has no attribute 'request')

    Models



    class Profile(models.Model):
    phone = models.CharField(max_length=11, null=True, blank=True)
    education = models.ManyToManyField(Education, null=True, blank=True, related_name="education")
    full_name = models.CharField(max_length=30, null=True, blank=True)

    class Education(models.Model):
    degree = models.CharField(max_length=100, null=True, blank=True)
    school = models.CharField(max_length=100, null=True, blank=True)
    edu_start_date = models.DateField(null=True, blank=True)
    edu_end_date = models.DateField(null=True, blank=True)


    View



    class EducationView(CreateView):
    model = Education
    form_class = EducationForm
    pk_url_kwarg = 'pk'
    template_name = "profile_settings.html"

    class ProfileSettingsView(UpdateView):
    model = Profile
    form_class = ProfileSettingsForm
    pk_url_kwarg = 'pk'
    context_object_name = 'object'
    template_name = 'profile_settings.html'

    def get_success_url(self):
    return reverse_lazy('users:profile_settings', args = (self.object.id,))

    def get_object(self):
    pk = self.kwargs.get('pk')
    return get_object_or_404(Profile, id=pk)

    def get_context_data(self, **kwargs):
    context = super(ProfileSettingsView, self).get_context_data(**kwargs)
    context['prof'] = self.get_object()
    return context


    Form



    class EducationForm(forms.ModelForm):

    degree = forms.CharField(max_length=40, required=False)
    school = forms.CharField(max_length=40, required=False)
    edu_start_date = forms.DateField(required=False,
    input_formats=settings.DATE_INPUT_FORMATS,
    widget=forms.DateInput(attrs='readonly': 'readonly'))
    edu_end_date = forms.DateField(required=False,
    input_formats=settings.DATE_INPUT_FORMATS,
    widget=forms.DateInput(attrs='readonly': 'readonly'))

    def save(self, commit=True):
    edu = super(EducationForm, self).save(commit=False)
    edu.save()
    profile = Profile.objects.get(self.request.user.profile.id)
    profile.education.add(education=edu)

    return edu


    class Meta:
    model = Education

    fields = ['degree','school','edu_start_date','edu_end_date']

    class ProfileSettingsForm(forms.ModelForm):
    class Meta:
    model = Profile

    fields = ['phone','full_name','education']

    widgets =

    'full_name': forms.TextInput('class': 'form-control'),
    'phone': forms.TextInput('class': 'form-control'),



    urls



    urlpatterns = [
    path('<int:pk>/education/update/', views.EducationUpdate.as_view(), name='education_update'),
    path('<int:pk>/profile/settings', views.ProfileSettingsView.as_view(), name='profile_settings'),
    ]


    Template (Profile_settings.html)



    <form action="#" style="padding-left: 15px; padding-right:15px;" method="POST">
    form.errors
    % csrf_token %
    <div class="row">
    <div id="education_update" class="modal fade" role="dialog">
    <div class="modal-dialog">

    <div class="modal-content">
    <div class="modal-header">
    <h4 class="modal-title">New Education</h4>
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    </div>
    <div class="modal-body">
    <div class="row">
    <div class="col-lg-12">
    <div class="form-group">
    <div class="field">
    <label>Degree title<span class="red-txt">*</span></label>
    <!-- <input class="form-control" type="text" name="degree" value="" maxlength="60" size="50"> -->
    edu_object.degree
    </div>
    </div>
    </div>
    <div class="col-lg-12">
    <div class="form-group">
    <div class="field">
    <label>School<span class="red-txt">*</span></label>
    <!-- <input class="form-control" type="text" name="school" value="" maxlength="60" size="50"> -->
    edu_object.school
    </div>
    </div>
    </div>

    <div class="col-lg-6">
    <div class="form-group">
    <div class="field">
    <label>Start date</label>
    <div id="datepicker1" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
    edu_object.edu_start_date
    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
    </div>
    </div>
    </div>
    </div>

    <div class="col-lg-6">
    <div class="form-group">
    <div class="field">
    <label>End date</label>
    <div id="datepicker2" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
    edu_object.edu_end_date
    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
    </div>
    </div>
    </div>
    </div>

    <div class="col-lg-12">
    <br>

    <div style="text-align:center;" class="login login-button">
    <input type="submit" class="btn btn-outline-primary btn-lg" style="cursor: pointer;" value="Save">
    </div>
    </div>
    </div>
    </div>
    </div>

    </div>
    </div>
    </div>
    </form>


    % if prof %
    <table class="table">
    <tbody>
    % for edu in prof.education.all %
    <tr class="divbutton" style="height: 90px;">
    <td>
    <div class="row">
    <div style="padding-left: 40px; font-size: 20px;"> edu.degree </div>
    <div style="padding-left: 40px; font-size: 20px;"> edu.school </div>
    </div>
    </td>
    <td class="align-middle">
    <div class="row">
    <div id="button_under" style="margin-right: 20px;" class="login login-button">
    <a href="#" class="btn btn-info" data-toggle="modal" data-target="#education_update" data-backdrop="false"><i class="fa fa-edit"></i> Edit</a>
    </div>
    <div id="button_under" class="login login-button">
    <a href="" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</a>
    </div>
    </div>
    </td>
    </tr>
    % endfor %
    % endif %
    </tbody>
    </table>









    share|improve this question
























      0












      0








      0








      • I have a profile model which contains education and created a many to
        many relation i.e many profiles can have many educations.

      • I have a template called Profile settings which contains multiple forms like Education CreateView, Education UpdateView and Education ListView. I'm using bootstrap modals to display these forms. Hence the they share same template.

      • Problem is whenever I save education form, it saves form in database
        and it shows in the education multiselect field of that profile in
        django admin but unselected, it does not create a m2m link between
        profile and education.

      • I overrided the save() method to create the link as suggested by the community but I'm not able to get the current profile. Getting this error after saving. ('EducationForm' object has no attribute 'request')

      Models



      class Profile(models.Model):
      phone = models.CharField(max_length=11, null=True, blank=True)
      education = models.ManyToManyField(Education, null=True, blank=True, related_name="education")
      full_name = models.CharField(max_length=30, null=True, blank=True)

      class Education(models.Model):
      degree = models.CharField(max_length=100, null=True, blank=True)
      school = models.CharField(max_length=100, null=True, blank=True)
      edu_start_date = models.DateField(null=True, blank=True)
      edu_end_date = models.DateField(null=True, blank=True)


      View



      class EducationView(CreateView):
      model = Education
      form_class = EducationForm
      pk_url_kwarg = 'pk'
      template_name = "profile_settings.html"

      class ProfileSettingsView(UpdateView):
      model = Profile
      form_class = ProfileSettingsForm
      pk_url_kwarg = 'pk'
      context_object_name = 'object'
      template_name = 'profile_settings.html'

      def get_success_url(self):
      return reverse_lazy('users:profile_settings', args = (self.object.id,))

      def get_object(self):
      pk = self.kwargs.get('pk')
      return get_object_or_404(Profile, id=pk)

      def get_context_data(self, **kwargs):
      context = super(ProfileSettingsView, self).get_context_data(**kwargs)
      context['prof'] = self.get_object()
      return context


      Form



      class EducationForm(forms.ModelForm):

      degree = forms.CharField(max_length=40, required=False)
      school = forms.CharField(max_length=40, required=False)
      edu_start_date = forms.DateField(required=False,
      input_formats=settings.DATE_INPUT_FORMATS,
      widget=forms.DateInput(attrs='readonly': 'readonly'))
      edu_end_date = forms.DateField(required=False,
      input_formats=settings.DATE_INPUT_FORMATS,
      widget=forms.DateInput(attrs='readonly': 'readonly'))

      def save(self, commit=True):
      edu = super(EducationForm, self).save(commit=False)
      edu.save()
      profile = Profile.objects.get(self.request.user.profile.id)
      profile.education.add(education=edu)

      return edu


      class Meta:
      model = Education

      fields = ['degree','school','edu_start_date','edu_end_date']

      class ProfileSettingsForm(forms.ModelForm):
      class Meta:
      model = Profile

      fields = ['phone','full_name','education']

      widgets =

      'full_name': forms.TextInput('class': 'form-control'),
      'phone': forms.TextInput('class': 'form-control'),



      urls



      urlpatterns = [
      path('<int:pk>/education/update/', views.EducationUpdate.as_view(), name='education_update'),
      path('<int:pk>/profile/settings', views.ProfileSettingsView.as_view(), name='profile_settings'),
      ]


      Template (Profile_settings.html)



      <form action="#" style="padding-left: 15px; padding-right:15px;" method="POST">
      form.errors
      % csrf_token %
      <div class="row">
      <div id="education_update" class="modal fade" role="dialog">
      <div class="modal-dialog">

      <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title">New Education</h4>
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
      <div class="row">
      <div class="col-lg-12">
      <div class="form-group">
      <div class="field">
      <label>Degree title<span class="red-txt">*</span></label>
      <!-- <input class="form-control" type="text" name="degree" value="" maxlength="60" size="50"> -->
      edu_object.degree
      </div>
      </div>
      </div>
      <div class="col-lg-12">
      <div class="form-group">
      <div class="field">
      <label>School<span class="red-txt">*</span></label>
      <!-- <input class="form-control" type="text" name="school" value="" maxlength="60" size="50"> -->
      edu_object.school
      </div>
      </div>
      </div>

      <div class="col-lg-6">
      <div class="form-group">
      <div class="field">
      <label>Start date</label>
      <div id="datepicker1" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
      edu_object.edu_start_date
      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
      </div>
      </div>
      </div>
      </div>

      <div class="col-lg-6">
      <div class="form-group">
      <div class="field">
      <label>End date</label>
      <div id="datepicker2" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
      edu_object.edu_end_date
      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
      </div>
      </div>
      </div>
      </div>

      <div class="col-lg-12">
      <br>

      <div style="text-align:center;" class="login login-button">
      <input type="submit" class="btn btn-outline-primary btn-lg" style="cursor: pointer;" value="Save">
      </div>
      </div>
      </div>
      </div>
      </div>

      </div>
      </div>
      </div>
      </form>


      % if prof %
      <table class="table">
      <tbody>
      % for edu in prof.education.all %
      <tr class="divbutton" style="height: 90px;">
      <td>
      <div class="row">
      <div style="padding-left: 40px; font-size: 20px;"> edu.degree </div>
      <div style="padding-left: 40px; font-size: 20px;"> edu.school </div>
      </div>
      </td>
      <td class="align-middle">
      <div class="row">
      <div id="button_under" style="margin-right: 20px;" class="login login-button">
      <a href="#" class="btn btn-info" data-toggle="modal" data-target="#education_update" data-backdrop="false"><i class="fa fa-edit"></i> Edit</a>
      </div>
      <div id="button_under" class="login login-button">
      <a href="" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</a>
      </div>
      </div>
      </td>
      </tr>
      % endfor %
      % endif %
      </tbody>
      </table>









      share|improve this question














      • I have a profile model which contains education and created a many to
        many relation i.e many profiles can have many educations.

      • I have a template called Profile settings which contains multiple forms like Education CreateView, Education UpdateView and Education ListView. I'm using bootstrap modals to display these forms. Hence the they share same template.

      • Problem is whenever I save education form, it saves form in database
        and it shows in the education multiselect field of that profile in
        django admin but unselected, it does not create a m2m link between
        profile and education.

      • I overrided the save() method to create the link as suggested by the community but I'm not able to get the current profile. Getting this error after saving. ('EducationForm' object has no attribute 'request')

      Models



      class Profile(models.Model):
      phone = models.CharField(max_length=11, null=True, blank=True)
      education = models.ManyToManyField(Education, null=True, blank=True, related_name="education")
      full_name = models.CharField(max_length=30, null=True, blank=True)

      class Education(models.Model):
      degree = models.CharField(max_length=100, null=True, blank=True)
      school = models.CharField(max_length=100, null=True, blank=True)
      edu_start_date = models.DateField(null=True, blank=True)
      edu_end_date = models.DateField(null=True, blank=True)


      View



      class EducationView(CreateView):
      model = Education
      form_class = EducationForm
      pk_url_kwarg = 'pk'
      template_name = "profile_settings.html"

      class ProfileSettingsView(UpdateView):
      model = Profile
      form_class = ProfileSettingsForm
      pk_url_kwarg = 'pk'
      context_object_name = 'object'
      template_name = 'profile_settings.html'

      def get_success_url(self):
      return reverse_lazy('users:profile_settings', args = (self.object.id,))

      def get_object(self):
      pk = self.kwargs.get('pk')
      return get_object_or_404(Profile, id=pk)

      def get_context_data(self, **kwargs):
      context = super(ProfileSettingsView, self).get_context_data(**kwargs)
      context['prof'] = self.get_object()
      return context


      Form



      class EducationForm(forms.ModelForm):

      degree = forms.CharField(max_length=40, required=False)
      school = forms.CharField(max_length=40, required=False)
      edu_start_date = forms.DateField(required=False,
      input_formats=settings.DATE_INPUT_FORMATS,
      widget=forms.DateInput(attrs='readonly': 'readonly'))
      edu_end_date = forms.DateField(required=False,
      input_formats=settings.DATE_INPUT_FORMATS,
      widget=forms.DateInput(attrs='readonly': 'readonly'))

      def save(self, commit=True):
      edu = super(EducationForm, self).save(commit=False)
      edu.save()
      profile = Profile.objects.get(self.request.user.profile.id)
      profile.education.add(education=edu)

      return edu


      class Meta:
      model = Education

      fields = ['degree','school','edu_start_date','edu_end_date']

      class ProfileSettingsForm(forms.ModelForm):
      class Meta:
      model = Profile

      fields = ['phone','full_name','education']

      widgets =

      'full_name': forms.TextInput('class': 'form-control'),
      'phone': forms.TextInput('class': 'form-control'),



      urls



      urlpatterns = [
      path('<int:pk>/education/update/', views.EducationUpdate.as_view(), name='education_update'),
      path('<int:pk>/profile/settings', views.ProfileSettingsView.as_view(), name='profile_settings'),
      ]


      Template (Profile_settings.html)



      <form action="#" style="padding-left: 15px; padding-right:15px;" method="POST">
      form.errors
      % csrf_token %
      <div class="row">
      <div id="education_update" class="modal fade" role="dialog">
      <div class="modal-dialog">

      <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title">New Education</h4>
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
      <div class="row">
      <div class="col-lg-12">
      <div class="form-group">
      <div class="field">
      <label>Degree title<span class="red-txt">*</span></label>
      <!-- <input class="form-control" type="text" name="degree" value="" maxlength="60" size="50"> -->
      edu_object.degree
      </div>
      </div>
      </div>
      <div class="col-lg-12">
      <div class="form-group">
      <div class="field">
      <label>School<span class="red-txt">*</span></label>
      <!-- <input class="form-control" type="text" name="school" value="" maxlength="60" size="50"> -->
      edu_object.school
      </div>
      </div>
      </div>

      <div class="col-lg-6">
      <div class="form-group">
      <div class="field">
      <label>Start date</label>
      <div id="datepicker1" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
      edu_object.edu_start_date
      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
      </div>
      </div>
      </div>
      </div>

      <div class="col-lg-6">
      <div class="form-group">
      <div class="field">
      <label>End date</label>
      <div id="datepicker2" class="datepicker input-group date" data-date-format="yyyy-mm-dd">
      edu_object.edu_end_date
      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
      </div>
      </div>
      </div>
      </div>

      <div class="col-lg-12">
      <br>

      <div style="text-align:center;" class="login login-button">
      <input type="submit" class="btn btn-outline-primary btn-lg" style="cursor: pointer;" value="Save">
      </div>
      </div>
      </div>
      </div>
      </div>

      </div>
      </div>
      </div>
      </form>


      % if prof %
      <table class="table">
      <tbody>
      % for edu in prof.education.all %
      <tr class="divbutton" style="height: 90px;">
      <td>
      <div class="row">
      <div style="padding-left: 40px; font-size: 20px;"> edu.degree </div>
      <div style="padding-left: 40px; font-size: 20px;"> edu.school </div>
      </div>
      </td>
      <td class="align-middle">
      <div class="row">
      <div id="button_under" style="margin-right: 20px;" class="login login-button">
      <a href="#" class="btn btn-info" data-toggle="modal" data-target="#education_update" data-backdrop="false"><i class="fa fa-edit"></i> Edit</a>
      </div>
      <div id="button_under" class="login login-button">
      <a href="" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</a>
      </div>
      </div>
      </td>
      </tr>
      % endfor %
      % endif %
      </tbody>
      </table>






      python django django-models django-forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 9 at 7:33









      Ahsaan-566Ahsaan-566

      1449




      1449






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You'd need to pass the current user or request to the Form instance, so you could override get_form_kwargs in EducationView:



          def get_form_kwargs(self):
          kwargs = super(EducationView, self).get_form_kwargs()
          kwargs['request'] = self.request
          return kwargs


          and then initialize the EducationForm like this:



          def __init__(self, request, *args, **kwargs):
          super(EducationForm, self).__init__(*args, **kwargs)
          self.request = request


          As an aside, shouldn't the related_name field in Profile be profiles?






          share|improve this answer

























          • Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

            – Ahsaan-566
            Mar 9 at 8:57












          • You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

            – Mark R.
            Mar 9 at 9:04












          • solved the problem, thanks!

            – Ahsaan-566
            Mar 9 at 10:52











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075082%2fhow-do-create-a-custom-save-method-for-an-existing-m2m-field-django%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You'd need to pass the current user or request to the Form instance, so you could override get_form_kwargs in EducationView:



          def get_form_kwargs(self):
          kwargs = super(EducationView, self).get_form_kwargs()
          kwargs['request'] = self.request
          return kwargs


          and then initialize the EducationForm like this:



          def __init__(self, request, *args, **kwargs):
          super(EducationForm, self).__init__(*args, **kwargs)
          self.request = request


          As an aside, shouldn't the related_name field in Profile be profiles?






          share|improve this answer

























          • Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

            – Ahsaan-566
            Mar 9 at 8:57












          • You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

            – Mark R.
            Mar 9 at 9:04












          • solved the problem, thanks!

            – Ahsaan-566
            Mar 9 at 10:52















          1














          You'd need to pass the current user or request to the Form instance, so you could override get_form_kwargs in EducationView:



          def get_form_kwargs(self):
          kwargs = super(EducationView, self).get_form_kwargs()
          kwargs['request'] = self.request
          return kwargs


          and then initialize the EducationForm like this:



          def __init__(self, request, *args, **kwargs):
          super(EducationForm, self).__init__(*args, **kwargs)
          self.request = request


          As an aside, shouldn't the related_name field in Profile be profiles?






          share|improve this answer

























          • Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

            – Ahsaan-566
            Mar 9 at 8:57












          • You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

            – Mark R.
            Mar 9 at 9:04












          • solved the problem, thanks!

            – Ahsaan-566
            Mar 9 at 10:52













          1












          1








          1







          You'd need to pass the current user or request to the Form instance, so you could override get_form_kwargs in EducationView:



          def get_form_kwargs(self):
          kwargs = super(EducationView, self).get_form_kwargs()
          kwargs['request'] = self.request
          return kwargs


          and then initialize the EducationForm like this:



          def __init__(self, request, *args, **kwargs):
          super(EducationForm, self).__init__(*args, **kwargs)
          self.request = request


          As an aside, shouldn't the related_name field in Profile be profiles?






          share|improve this answer















          You'd need to pass the current user or request to the Form instance, so you could override get_form_kwargs in EducationView:



          def get_form_kwargs(self):
          kwargs = super(EducationView, self).get_form_kwargs()
          kwargs['request'] = self.request
          return kwargs


          and then initialize the EducationForm like this:



          def __init__(self, request, *args, **kwargs):
          super(EducationForm, self).__init__(*args, **kwargs)
          self.request = request


          As an aside, shouldn't the related_name field in Profile be profiles?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 10 at 9:34

























          answered Mar 9 at 8:08









          Mark R.Mark R.

          892411




          892411












          • Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

            – Ahsaan-566
            Mar 9 at 8:57












          • You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

            – Mark R.
            Mar 9 at 9:04












          • solved the problem, thanks!

            – Ahsaan-566
            Mar 9 at 10:52

















          • Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

            – Ahsaan-566
            Mar 9 at 8:57












          • You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

            – Mark R.
            Mar 9 at 9:04












          • solved the problem, thanks!

            – Ahsaan-566
            Mar 9 at 10:52
















          Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

          – Ahsaan-566
          Mar 9 at 8:57






          Getting this error: Request Method: POST Request URL: http://localhost:8000/users/education/create/ Exception Type: TypeError at /users/education/create/ Exception Value: cannot unpack non-iterable int object File "C:UsersHPDesktopCrowdsocialusersforms.py" in save 193. profile = Profile.objects.get(self.request.user.profile.id)`

          – Ahsaan-566
          Mar 9 at 8:57














          You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

          – Mark R.
          Mar 9 at 9:04






          You need to pass a field name to filter(), E.G., profile = Profile.objects.get(id=self.request.user.profile.id)

          – Mark R.
          Mar 9 at 9:04














          solved the problem, thanks!

          – Ahsaan-566
          Mar 9 at 10:52





          solved the problem, thanks!

          – Ahsaan-566
          Mar 9 at 10:52



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075082%2fhow-do-create-a-custom-save-method-for-an-existing-m2m-field-django%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко