Is there a common method/best practice/any means for combining forms that span multiple related models?
I want to create/edit model objects along with other, related model objects on the same page. Basically, being able to create/edit one model instance and another set of model instances related by a foreign key.
Not a great explanation, I know.
class Person(models.Model):
name = models.CharField(max_length=64, unique=True)
class PhoneNumber(models.Model):
person = models.ForeignKey(Person)
description = models.CharField(max_length=64, blank=True, null=True)
number = models.CharField(max_length=32, blank=True, null=True)
I want to be able to create/edit a person, along with all their associated phone numbers using a single form/page.
I've done this before using this nested form example, but it seems quite hackish.
models import User class InputUserInfo(forms. Form): phone = forms. CharField(max_length=20) instagram = forms. CharField(max_length=20) facebook = forms.
Django Model FormIt is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time. For example, suppose we have a model containing various fields, we don't need to repeat the fields in the form file.
A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. Let's say you have the following form: >>> from django import forms >>> class ArticleForm(forms.
Yes! Use formsets, specifically https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With