Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know this over a year old now but thought it may still be helpful to attempt an answer.</p> <p>I believe there may be a couple of issues here, but the main issue at hand is that the ModelForm is expecting a key to the database record not a string value.</p> <p>I would suggest making a Form instead of ModelForm, if you are making a form that isn't relating to your database. But if you do want to interact with your database then use the ModelForm and and define a ForeignKey in your Student model to relate to the language table, letting the ModelForm do the work for you.</p> <p>Example app based on my assumptions of your model.</p> <p><em><code>your_project/example_app/models.py:</code></em></p> <pre><code>from django.db import models class Student(models.Model): name = models.CharField(max_length=255) language = models.ForeignKey('Language') class Language(models.Model): name = models.CharField(max_length=255) </code></pre> <p><em><code>your_project/example_app/forms.py:</code></em></p> <pre><code>from django.forms import ModelForm from example.models import Student class StudentForm(ModelForm): class Meta: model = Student </code></pre> <p>Relevant docs: <a href="https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform" rel="nofollow noreferrer">https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform</a></p> <p>snippet:</p> <blockquote> <p>As you might expect, the ForeignKey and ManyToManyField model field types are special cases:</p> <p>ForeignKey is represented by <code>django.forms.ModelChoiceField</code>, which is a ChoiceField whose choices are a model QuerySet. ManyToManyField is represented by <code>django.forms.ModelMultipleChoiceField</code>, which is a MultipleChoiceField whose choices are a model QuerySet.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload