title: Django-limit_choices_to With ForeignKey based on different links
body: <p>I am making a building manager website in Django. I have a page that is a <code>DetailView</code> for each building. On the <code>DetailView</code> page is a link to add a work task for repairs. On creating a work task I would like it to filter only for the building page that I came from. For example, from &quot;building/1&quot; page clicking the work task link would only allow you to see and select rooms in the building with pk = 1.
  views.py</p>
  <pre><code>from django.shortcuts import render
  from .models import Building, Room, Task, Task_Note
  from django.views.generic.list import ListView
  from django.views.generic.detail import DetailView
  from django.views.generic.edit import CreateView
  
  # Create your views here.
  class BuildingList(ListView):
      model = Building
      template_name = &quot;dorm/buildings.html&quot;
  
  class BuildingDetail(DetailView):
      model = Building
      template_name = &quot;dorm/building.html&quot;
  
  class TaskAdd(CreateView):
      model = Task
      fields = [&quot;time&quot;, &quot;room&quot;, &quot;work_request_id&quot;, &quot;description&quot;]
      template_name = &quot;dorm/add_task.html&quot;
  </code></pre>
  <p>models.py</p>
  <pre><code> from django.db import models
      from django.db.models.fields import DateTimeField, IntegerField, CharField, TextField
      from django.db.models.fields.related import ForeignKey
      import datetime
      
      from django.urls.base import reverse
      error = &quot;Error&quot;
      # Create your models here.
      class Building(models.Model):
          number = IntegerField(null = True, blank = True)
      
          @property
          def number_of_rooms(self):
              try:
                  return Room.objects.filter(building = self).count()
              except:
                  return error
          @property
          def number_of_active_tasks(self):
                  x = 0
                  q = Task.objects.all()
                  for i in range(len(q)):
                      if q[i].building == str(self) and q[i].status == &quot;A&quot;:
                          x += 1
                  return x
      
      
          def __str__(self):
              return f&quot;{self.number}&quot;
              
      
      
      class Room(models.Model):
          number = IntegerField(null = True, blank = True)
          building = ForeignKey(Building, null = True, blank = True, on_delete = models.CASCADE)
      
          def __str__(self):
              return f&quot;{self.building}/{self.number}&quot;
      
      class Task(models.Model):
          room = ForeignKey(Room, null = True, blank = True, on_delete = models.CASCADE)
          work_request_id = IntegerField(null = True, blank = True)
          time = DateTimeField(null = True, blank = True, default = datetime.datetime.now())
          description = CharField(max_length = 1000, null = True, blank = True)
          STATUS_CHOICES = [
              (&quot;A&quot;, &quot;Active&quot;),
              (&quot;C&quot;, &quot;Complete&quot;)
          ]
          status = CharField(max_length = 1, null = True, blank = True, choices = STATUS_CHOICES, default = &quot;A&quot;)
          @property
          def building(self):
              try:
                  return f&quot;{self.room.building}&quot;
              except:
                  return error
      
          def __str__(self):
              return f&quot;{self.work_request_id}&quot;
          def get_absolute_url(self):
              return reverse(&quot;buildings&quot;)
      
      
      #________________________________NOTES__________________________________
      class Task_Note(models.Model):
          task = ForeignKey(Task, null = True, blank = True, on_delete = models.CASCADE)
          time = DateTimeField(null = True, blank = True)
          description = TextField(null = True, blank = True)
      
          def __str__(self):
              return f&quot;{self.task.pk}&quot;
  
     
  </code></pre>