I'm using Django v4.1
and have a foreignKey relationship I want to pull into a form.
The two models:
class Service(models.Model):
client = models.ForeignKey("Client", on_delete=models.SET_NULL, null=True)
# some other fields
and
class Client(models.Model):
name = models.CharField(max_length=50)
# a few more fields
The form:
class ServiceForm(forms.ModelForm):
class Meta:
model = Service
fields = "__all__"
However, in the front-facing form, "Client" is generated, but without a SELECT
field.
There is another model with a foreignKey relationship to Client, order:
class Order(models.Model):
client = models.ForeignKey("Client", on_delete=models.SET_NULL, null=True)
# more fields...
And it's form:
class OrderModelForm(forms.ModelForm):
class Meta:
model = Order
fields = "__all__"
Which renders as expected with a SELECT
field for the Client
.
Looking at the forms docs I experimented with (importing Client
from models
and) adding a ModelChoiceField,
("client": forms.ModelChoiceField(queryset=Client.objects.all())
both inside and outside of a widgets
dict), but based on this SO post I'm thinking Django should be rendering that on it's own, as it is for Order
.
Please share suggestions in debugging. Thanks much.