问题
i tried many ways and searched alot(googled) but no one worked for me . whenever i save my inlineformset it only save the last form , 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)
this is my view
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 does i have missed something ? or i maybe i do something wrongly ?! and i'm ready to pay to get it working thanks for any advice
回答1:
It is several years since I did any work on Django but I will have a go at annotating your code and adding my thoughts.
def form_valid(self,form):
context = self.get_context_data() # This line looks fine
context = context['book'] # You are overwriting your context here, call this
with transaction.atomic():
self.object = form.save() # Your have a save here and one further down, this is also setting self object which may be incorrect.
if context.is_valid():
context.instance = self.object
context.save()
return super(CreateBookView,self).form_valid(form)
Like I said, I haven't done any Django in several years, so I am just referring to some of my old code (which may be outdated) from https://github.com/timhughes/django-cbv-inline-formset/blob/master/music/views.py#L26 which I will annotate with comments.
def form_valid(self, form):
context = self.get_context_data(form=form) # Get the context data for the form in question.
formset = context['track_formset'] # Extract the formset out of the context.
if formset.is_valid(): # Validate the formset is valid
response = super().form_valid(form) # Call the parent class's form_valid.
formset.instance = self.object # Set the formsets type (instance of self.object) so that django knows how to save it.
formset.save() # Save the formset here
return response # Send a response to the user
else:
return super().form_invalid(form) # Send a response to the user with invalid form information.
来源:https://stackoverflow.com/questions/62252867/inline-formset-only-save-the-last-form