django:adding more than one value in model field
<p>i want to create a app for classroom in which their only one teacher and students can be more than one.a nd student can be in more than one class. i want to store usernames of students in one classroom. is there any model field i can use to store usernames of students.
my code so far:</p>

<p>models.py </p>

<pre><code>from django.db import models
from django.conf import settings
# Create your models here.

class classroom(models.Model):
    teacher = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    students = models.CharField(max_length=120)
</code></pre>

<p>Create a Student model and add a many to many key to a ClassRoom instance. In that way you are telling "<strong>A student may have several  classroom and a classroom may have many students</strong>"</p>

<pre><code>class ClassRoom(models.Model):
    teacher = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)


 class Student(models.Model):
    name = models.CharField(max_length=120)
    classroom =  models.ManyToManyField(ClassRoom, related_name="students")
</code></pre>

<p>So, if you have a <code>classroom</code> instance and want to get all its student. Notice the related_name attribute in the field classroom. </p>

<pre><code>classroom.students.all()
</code></pre>

<p>If you don't add <code>related_name</code> attribute, you will need to use <code>_set</code> notation</p>

<pre><code>classroom.student_set.all()
</code></pre>

<p>If you have a student instance and want to get his classroom</p>

<pre><code>student.classroom
</code></pre>

<p>TIP: For class model naming, use CamelCase notation. </p>

python django django-models django-forms django-views