Django inlineformsetfactory - why is this useful?

Sorry for the newbies question, but ...

Can someone shed light on what is used for inlineformset_factory?

I gave an example from the Django documentation:

#Models
class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=100)

#View

def jojo(request):

    BookFormSet = inlineformset_factory(Author, Book)
    author = Author.objects.get(name=u'Mike Royko')
    formset = BookFormSet(instance=author)


    return render_to_response('jojo.html', {
        'formset': formset,
    })

#jojo.html
<form action="" method="POST">
<table>

{{ formset }}

</table>
<input type="submit" value="Submit" />
</form>

But it only displays the margins of the book.

I realized that formet would display the Book form with the author’s inline form as Django Admin. Also, I can't easily pass initial values ​​to formet?

Then what is the best way to use two separate AuthorForm and BookForm?

Or am I missing something obvious?

+3
source share
3 answers

inlineformset_factory ( modelformset_factory) - . , .

formet_factory , () ( ) .

+2

inlineformset_factory , , .

inlineformset_factory , :

views.py

from django.shortcuts import get_object_or_404, render_to_response
from django.forms.models import inlineformset_factory
from django.http import HttpResponseRedirect
from django.template import RequestContext

from App_name.models import * #E.g. Main, Nested, MainForm, etc.

.,.

@login_required
def Some_view(request, main_id=None, redirect_notice=None):
    #login stuff . . .
    c = {}
    c.update(csrf(request))
    c.update({'redirect_notice':redirect_notice})#Redirect notice is an optional argument I use to send user certain notifications, unrelated to this inlineformset_factory example, but useful.

    #Intialization --- The start of the view specific functions
    NestedFormset = inlineformset_factory(Main, Nested, can_delete=False, )
    main = None
    if main_id :
        main = Main.objects.get(id=main_id)#get_object_or_404 is also an option

    # Save new/edited Forms
    if request.method == 'POST':
        main_form = MainForm(request.POST, instance=main, prefix='mains')
        formset = NestedFormset(request.POST, request.FILES, instance=main, prefix='nesteds')
        if main_form.is_valid() and formset.is_valid():
            r = main_form.save(commit=False)
            #do stuff, e.g. setting any values excluded in the MainForm
            formset.save()
            r.save()
            return HttpResponseRedirect('/Home_url/')
    else:
        main_form = MainForm(instance=main, prefix='mains') #initial can be used in the MainForm here like normal.
        formset = NestedFormset(instance=main, prefix='nesteds')
    c.update({'main_form':main_form, 'formset':formset, 'realm':realm, 'main_id':main_id})
    return render_to_response('App_name/Main_nesteds.html', c, context_instance=RequestContext(request))

template.html

{% if main_form %}
<form action="." method="POST">{% csrf_token %}
    {{ formset.management_form }}
    <table>
        {{main_form.as_table}}
        {% for form in formset.forms %}
            <table>{{ form }}</table>
        {% endfor %}
    </table>
    <p><input type="submit" name="submit" value="Submit" class="button"></p>
</form>
{% endif %}
+2

inlineformset_factory .

, , :

  • .
  • .

With some JavaScript code, you can add the "add another line" functionality.

+1
source

Source: https://habr.com/ru/post/1725835/


All Articles