title: Unable to use data of one app in another in Django
body: <p>I have three apps (Internship, UserProfile and Infrastructure) in my django project. I have made models Profile and StudentProject in UserProfile Model. The StudentProject Model contains two foreign key-> user and Lab (this model is defined in the Infrastructure model). In a template(details.html file) in Infrastructure app, i want to retrieve all StudentProjects who have their foreign key as the lab whose details are currently being shown. I am unable to bring the student projects created here. Please help someone. I have already tried to use filter but it doesn't work!</p>
  
  <p><strong>userprofile/models.py file</strong></p>
  
  <pre><code>from django.conf import settings
  from django.db import models
  from django.core.urlresolvers import reverse
  from infrastructure.models import Lab
  
  
  class Profile(models.Model):
      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
      name = models.CharField(max_length=100)
      type = models.CharField(max_length=100)
      profile_picture = models.FileField()
      skills = models.CharField(max_length=1000)
  
      def get_absolute_url(self):
          return reverse('userprofile:index')
  
      def __str__(self):
          return self.name
  
  
  class StudentProject(models.Model):
      user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
      title = models.CharField(max_length=100)
      project_picture = models.FileField()
      lab = models.ForeignKey(Lab)
      mentor = models.CharField(max_length=100)
      domain = models.CharField(max_length=100)
      description = models.CharField(max_length=1000)
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return reverse('userprofile:index')
  </code></pre>
  
  <p><strong>infrastructure/models.py file</strong></p>
  
  <pre><code>from django.db import models
  from django.core.urlresolvers import reverse
  
  
  class Lab(models.Model):
      name = models.CharField(max_length=100)
      department = models.CharField(max_length=100)
      description = models.CharField(max_length=1000)
      lab_logo = models.FileField()
  
      def get_absolute_url(self):
          return reverse('infrastructure:details', kwargs={'pk': self.pk})
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p><strong>infrastructure/templates/details.html</strong>
  <strong><em>->This is where i want all student projects of lab to be shown</em></strong></p>
  
  <pre><code>{% extends 'infrastructure/base.html' %}
  {% block body %}
      &lt;h1&gt;This is details page&lt;/h1&gt;
      &lt;img src="{{ lab.lab_logo.url }}" style="width: 300px;"&gt;
      &lt;h1&gt;{{ lab.name }}&lt;/h1&gt;
      &lt;h2&gt;{{lab.projects}}&lt;/h2&gt;
  {% endblock %}
  </code></pre>
  
  <p><strong>infrastructure/views.py file</strong></p>
  
  <pre><code>from django.core.urlresolvers import reverse_lazy
  from django.views import generic
  from infrastructure.models import Lab
  from django.views.generic.edit import CreateView, UpdateView, DeleteView
  
  
  class IndexView(generic.ListView):
      template_name = 'infrastructure/index.html'
      context_object_name = 'all_labs'
  
      def get_queryset(self):
          return Lab.objects.all()
  
  
  class DetailView(generic.DetailView):
      model = Lab
      template_name = 'infrastructure/details.html'
  
  
  class LabCreate(generic.CreateView):
      model = Lab
      fields = ['name', 'department', 'description', 'lab_logo']
  
  
  class LabUpdate(generic.UpdateView):
      model = Lab
      fields = ['name', 'department', 'description', 'lab_logo']
  
  
  class LabDelete(DeleteView):
      model = Lab
      success_url = reverse_lazy('infrastructure:index')
  </code></pre>
  
  <p><strong>infrastructure/urls.py file</strong></p>
  
  <pre><code>from django.conf.urls import url
  
  from . import views
  
  app_name = 'infrastructure'
  
  urlpatterns = [
      url(r'^$', views.IndexView.as_view(), name='index'),
      url(r'^(?P&lt;pk&gt;[0-9]+)/$', views.DetailView.as_view(), name='details'),
  
      url(r'^lab/add/$', views.LabCreate.as_view(), name='lab-add'),
      url(r'^lab/(?P&lt;pk&gt;[0-9]+)/$', views.LabUpdate.as_view(), name='lab-update'),
  
      url(r'^lab/(?P&lt;pk&gt;[0-9]+)/delete/$', views.LabDelete.as_view(), name='lab-delete'),
  ]
  </code></pre>
  
  <p><strong>userprofile/views.py file</strong></p>
  
  <pre><code>from django.contrib.auth import login, authenticate, logout
  from django.http import request
  from django.shortcuts import render, redirect, get_object_or_404
  from django.urls import reverse_lazy
  
  from .models import Profile, StudentProject
  from django.views.generic.edit import CreateView, UpdateView, DeleteView
  from .forms import UserForm
  from django.views import generic
  from django.views.generic import View
  
  
  class IndexView(generic.TemplateView):
      template_name = 'userprofile/index.html'
      context_object_name = 'user_profile'
  
      def get_queryset(self):
          return request.user.get_profile()
  
  
  class ProfileCreate(CreateView):
      model = Profile
      fields = ['user', 'name', 'type', 'profile_picture', 'skills']
  
  
  class ProfileUpdate(UpdateView):
      model = Profile
      fields = ['user', 'name', 'type', 'profile_picture', 'skills']
  
  
  class StudentProjectCreate(CreateView):
      model = StudentProject
      fields = ['user', 'title', 'project_picture', 'lab', 'mentor', 'domain', 'description']
  
  
  class StudentProjectUpdate(UpdateView):
      model = StudentProject
      fields = ['user', 'title', 'project_picture', 'lab', 'mentor', 'domain', 'description']
  
  
  class StudentProjectDelete(DeleteView):
      model = StudentProject
      success_url = reverse_lazy('userprofile:index')
  
  
  def logout_view(request):
      logout(request)
      return render(request, 'userprofile/logout.html')
  
  
  # for new user
  class UserFormView(View):
      form_class = UserForm
      template_name= 'internship/registration_form.html'
  
      # display blank form
      def get(self, request):
          form = self.form_class(None)
          return render(request, self.template_name, {'form': form})
  
      # process from data
      def post(self, request):
          form = self.form_class(request.POST)
  
          if form.is_valid():
              user = form.save(commit=False)
  
              # cleaned (normalized) data
              username = form.cleaned_data['username']
              password = form.cleaned_data['password']
              user.set_password(password)
              user.save()
  
              # return User object if credentials are correct
              user = authenticate(username=username, password=password)
              if user is not None:
                  if user.is_active:
                      login(request, user)
                      return redirect('userprofile:profile-add')
  
          return render(request, self.template_name, {'form': form})
  </code></pre>
  
  <p><strong>userprofile/urls.py file</strong></p>
  
  <pre><code>from django.conf.urls import url, include
  from django.contrib.auth import views as auth_views
  
  from userprofile import views
  
  app_name = 'userprofile'
  
  
  urlpatterns = [
      url(r'^$', views.IndexView.as_view(), name='index'),
      url(r'^profile/add/$', views.ProfileCreate.as_view(), name='profile-add'),
      url(r'^profile/(?P&lt;pk&gt;[0-9]+)/$', views.ProfileUpdate.as_view(), name='profile-update'),
      url(r'^logout$', views.logout_view, name='logout_view'),
      url(r'^login/$', auth_views.login, {'template_name': 'userprofile/login.html'}),
      url(r'^register/$', views.UserFormView.as_view(), name='register'),
      url(r'^studentproject/add/$', views.StudentProjectCreate.as_view(), name='student-project-add'),
      url(r'^studentproject/(?P&lt;pk&gt;[0-9]+)/$', views.StudentProjectUpdate.as_view(), name='student-project-update'),
      url(r'^studentproject/(?P&lt;pk&gt;[0-9]+)/delete/$', views.StudentProjectDelete.as_view(), name='student-project-delete'),
  ]
  </code></pre>