title: How to check if the object is assigned to another object | Django
body: <p>I am working on a django case like below:<br>
  <strong>models.py</strong></p>
  <pre><code>class Room(models.Model):
      name = models.CharField(&quot;Room No.&quot;,max_length=200)
  
      class Meta:
          verbose_name_plural = &quot;Room&quot;
  
      def __str__(self):
          return self.name
  
  class Student(models.Model):
      name = models.CharField(&quot;name&quot;,max_length=200)
      father_name = models.CharField(&quot;father Name&quot;,max_length=200)
      cell_no = models.CharField(&quot;cell No&quot;,max_length=200)
      address = models.CharField(&quot;address&quot;,max_length=500)
      room = models.ForeignKey(Room, on_delete=models.CASCADE, null=True, blank=True, related_name='all_rooms')
  
      class Meta:
          verbose_name_plural = &quot;Student&quot;
  
      def __str__(self):
          return self.name
  </code></pre>
  <p><strong>views.py</strong> <br></p>
  <pre><code>def room(request):
      allrooms= Room.objects.all()
  
      form = RoomForm(request.POST or None, request.FILES or None)
  
      if form.is_valid():
          form.save()
          messages.success(request, &quot;Room added successfully.&quot;)
          return redirect('/room')
      context = {'allrooms':allrooms, 'form':form}
      return render(request, 'room.html', context)
  </code></pre>
  <p>In <strong>templates</strong> in room.html I want to show the status Vacant/Occupied based on the fact if a room is assigned to some student or not. I have the following code in template but it shows 'Vacant' status for all rooms.</p>
  <pre><code>&lt;table id=&quot;example1&quot; class=&quot;table table-bordered table-striped&quot;&gt;
                    &lt;thead&gt;
                       &lt;tr&gt;
                          &lt;th&gt;Room&lt;/th&gt;
                          &lt;th class=&quot;text-center&quot;&gt;Status&lt;/th&gt;
                          &lt;th class=&quot;text-center&quot;&gt;Action&lt;/th&gt;
                       &lt;/tr&gt;
                    &lt;/thead&gt;
                    &lt;tbody&gt;
                       {% for room in allrooms %}
                       &lt;tr&gt;
                          &lt;td&gt;{{ room.name }}&lt;/td&gt;
                          &lt;td class=&quot;text-center&quot;&gt;
                             {% if room.student_set.all %}
                             &lt;small class=&quot;badge badge-danger&quot;&gt;Occupied&lt;/small&gt;
                             {% elif not room.student.all %}
                             &lt;small class=&quot;badge badge-success&quot;&gt;Vacant&lt;/small&gt;
                             {% endif %}
                          &lt;/td&gt;
                          &lt;td class=&quot;text-center&quot;&gt;&lt;a href=&quot;{% url 'updateroom' room.id %}&quot;&gt;&lt;i class=&quot;fas fa-edit&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                       {% endfor %}
                    &lt;/tbody&gt;
                 &lt;/table&gt;
  </code></pre>
  <p>Please help someone to show he status of the room.</p>