how to make django inlineformset template

孤人 提交于 2020-06-23 16:48:07

问题


Whenever I save my inlineformset, it only saves the last form, if count=3 then my front end generates 3 fields to add books, but it only the save the last one.

my models.py

class Book(models.Model):
   book = models.CharField(max_length=20,unique=True)
   author = models.ForeignKey(Author,on_delete=models.CASCADE)

class Author(models.Model):
   author = models.CharField(max_length=30,unique=True)
   count = models.IntegerField()

this is my forms.py

class AuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ['author','count']
class BookForm(ModelForm):
    class Meta:
        model = Book
        fields = ['book']
InlineFormset_Author = inlineformset_factory(Author,Book,form=BookForm,extra=1)

and also this is my views.py

class CreateBookView(LoginRequiredMixin,SuccessMessageMixin,CreateView):
    model = Author
    form_class = AuthorForm

   def get_context_data(self,*args,**kwargs):
        context = super(CreateBookView,self).get_context_data(*args,**kwargs)
        if self.request.POST:
            context['book'] = InlineFormset_Author(self.request.POST)
        context['book'] = InlineFormset_Author()
        return context

   def form_valid(self,form):
        context = self.get_context_data()
        context = context['book']
        with transaction.atomic():
            self.object = form.save()
            if context.is_valid():
                context.instance = self.object
                context.save()
        return super(CreateBookView,self).form_valid(form)

and this is my template

<form method="POST">{% csrf_token %}
  {{book.management_form}}
  {{form.author | add_class:'form-control col-12 col-sm-10 mx-auto'}}
  {{form.count | add_class:'form-control col-12 col-sm-10 mx-auto' | attr:'id:count'}}
  <button class="col-4 mx-auto  shadow-lg border-right border-left">insert</button>
  <div id="BOOK" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="my-modal-title" aria-hidden="true">

    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="my-modal-title">BOOK</h5>
          <p class="close" data-dismiss="modal" aria-label="Close">
          <div class="modal-body">
            <button type="submit" class="btn btn-success">save</button></dic>
          </div>
          <div class='modal-footer'>
</form>
<script>
    $(document).ready(function(){
    $('#BOOKBTN').on('click',function () {
    let allValue=[];
    let numberOfInput=$('#count').val();
    let allContent='';
    let justforName=0;
    let numOfContent=$('.modal-body input').length;
    for(let j=0;j<numOfContent;j++){
    justforName=j+1;

    allValue.push($('input[name="BOOK'+justforName+'"').val());

    }
    if(numOfContent!=numberOfInput){
    for(let i=0;i<numberOfInput;i++){
    justforName=i+1;
    {% for i in book.forms %}
      allContent+='{{i.book}}';
      {% endfor %}
    }
    $('.modal-body').html(allContent);
    }
    for(let j=0;j<allValue.length;j++){
    justforName=j+1;

    $('input[name="BOOK'+justforName+'"').val(allValue[j])
    }})})
</script>

I have used widget tweaks to provide classes and ids to my input fields. Have I missed something? Have I done something wrong?

and i'm ready to pay to get it working thanks for any advice

来源:https://stackoverflow.com/questions/62285767/how-to-make-django-inlineformset-template

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