A ModelForm is:
a helper class that lets you create a Form class from a Django model.
An idea would be to write a custom model Field to address this issue. However, according to the relevant chapter of Writing Custom Model Fields
Fields in a model must somehow be converted to fit into an existing database column type.
Trying to spot the available postgreSQL column types I found this page which does not seem to provide a solution.
On the other hand, there is an old but updated SO post, which gives a solution that actually renders both forms in an HTML page and -this way- you gather all the needed fields in one form.
forms.py
class AccountForm(forms.ModelForm):
... Do stuff if necessary ...
class Meta:
model = Account
fields = ('the_fields', 'you_want')
class InstanceForm (forms.ModelForm):
... Do other stuff if necessary ...
class Meta:
model = Instance
fields = ('the_fields', 'you_want')
views.py
...
def register(request):
if request.method == 'POST':
account_form = AccountForm(request.POST)
instance_form = InstanceForm(request.POST)
if account_form.is_valid() and instance_form.is_valid():
account_form.save()
instance_form.save()
...
the_template.html
<form action="." method="post">
{% csrf_token %}
{{ account_form.as_p }}
{{ instance_form.as_p }}
<input type="submit" value="Submit">
</form>
This is also the idea of the admin's class InlineModelAdmin.
In the admin, for example, Account
will become an inline model to Instance
(which is a bad name for a django class).
admin.py:
from django.contrib import admin
class InstanceInline(admin.TabularInline):
model = Instance
class AccountAdmin(admin.ModelAdmin):
inlines = [
InstanceInline,
]