Creating or editing Django models
We’ve seen how to define a form, and in Chapter 2, Models and Migrations, you learned how to create Django model instances. By using these things together, you can build a view that displays a form and also saves a model instance to the database. This gives you an easy way to save data without having to write a lot of boilerplate code or create custom forms. In Bookr, we will use this method to allow users to add reviews without requiring access to the Django admin site. Without using ModelForm
, you can do something like this:
- You can create a form based on an existing model; for example,
Publisher
. The form would be calledPublisherForm
. - You can manually define the fields on
PublisherForm
, using the same rules defined on thePublisher
model, as shown here:class PublisherForm(forms.Form): name = forms.CharField(max_length=50) website = forms.URLField() …...