from django import forms from .models import City class CityForm(forms.Form): city = forms.ModelChoiceField(queryset=City.objects.all())
from django import forms from .models import Author, Book class AuthorForm(forms.Form): author = forms.ModelChoiceField(queryset=Author.objects.filter(book__isnull=False))In this example, we import both the Author and Book models and define a form called "AuthorForm" that contains a field called "author". The field is generated using the ModelChoiceField, which takes a queryset of all the Author objects that have at least one related Book object. Package library: Django.