Django:在表单中添加相关模型(ForeignKey)的字段

时间:2018-05-02 08:13:33

标签: django django-models django-forms

我有主要模型娱乐和相关型号EntertainmentCollage。现在我正在为我的模型做一个edditing页面,我需要转移它来编辑这两个模型。 我理解如何将一个表单转移到表单但是使用相关模型我很难。

   class Entertainment(models.Model):
        main_photo = models.ImageField(upload_to = 'where/')
        place = models.CharField(max_length=200)
        description = models.CharField(max_length=200)
        event_date = models.DateTimeField(auto_now_add=False,  blank=True, null = True) 

class EntertainmentCollage(models.Model):
    img = models.ImageField(upload_to = 'entertainment/portfolio', blank = True)
    album = models.ForeignKey(Entertainment, blank = True, null = True)

forms.py

class WhereCreateForm(ModelForm):
    class Meta:
        model = Entertainment
        fields = ['main_photo','place','description', 'event_date' ]

views.py

def edit_where(request, pk):
    place = Entertainment.objects.get(id=pk)

    form = WhereCreateForm(instance=place)

    if request.user.is_authenticated():
        if request.method == "POST":
            form = WhereCreateForm(request.POST, request.FILES, instance=place)

            if form.is_valid():
                form.save()                           
            return redirect('entertainment:where_list') 
    else:
        form = WhereCreateForm()
    return render(request, "entertainment/where_edit.html", {'form': form})

HTML

<form method = "post">
                    {% csrf_token %}
                        <p>{{ form.description }}</p>
                        <p>{{ form.place }}</p>
                        <p>{{ form.event_date }}</p>

                </div>
                <div class="col-md-9">
                    <section class="admin-section">
                        <div class="row">
                            <div class="col-md-4 admin__block" is-cover="false">
                                <div class="cover__wrapper edit__wrapper">
                                    <a class="delete-button">Delete</a>
                                    <a class="make-cover-button">Cover</a>
                                    <img src="img/place-placeholder-1.jpg" alt="">
                                </div>
                            </div>

                        </div>
                        <a href="#" class="button">Add photo</a>
                    </section>
                    <section>
                        <h4>Description</h4>
                        {{ form.description }}
                        <a href="#" class="button">Save</a>
                        <a href="#" class="button">Cancel</a>
                    </section>
                    </form>

1 个答案:

答案 0 :(得分:1)

正如@ art06在评论中所说,你可以使用inline formsets

您可以这样做:

from django.forms import inlineformset_factory

def edit_where(request, pk):
    place = Entertainment.objects.get(id=pk)
    FormSet2 = inlineformset_factory(Entertainment, EntertainmentCollage) 
    form = WhereCreateForm(instance=place)
    form2 = FormSet2() 

    if request.user.is_authenticated():
        if request.method == "POST":
            form = WhereCreateForm(request.POST, request.FILES, instance=place)
            form2 = FormSet2(request.POST or None, instance=place) 

            if form.is_valid():      
                if form2.is_valid():
                    form.save()  
                    form2.save()                    
            return redirect('entertainment:where_list') 
    else:
        form = WhereCreateForm()
        form2 = FormSet2() 
    return render(request, "entertainment/where_edit.html", {'form': form, 'form2': form2})

要将新表单添加到模板中,您可以使用:

{{ form2.management_form }}    
{% for frm in form2 %}
    {{ frm.as_table }}
{% endfor %}