title: Django - missing FROM-clause entry for table
body: <p>Okay, its been a while yet I can not solve or understand this problem. I also looked the other ones questions which has the same or similar title but I think this one seems different. I am not a pro in django and its admin face, but trying. So any help would be great. </p>
  
  <p>I am currently trying to build an admin interface for listing the fan profiles. ( there are fans and famous users ) And I was able to list them by their <code>deleted post count</code>. ie. they post something and this can be deleted by moderators but not users theirselves. And I wrote an sql query for it. </p>
  
  <pre><code>sql_deleted_post_count = """
          SELECT COUNT(*) 
          FROM posts_post 
          WHERE posts_post.state = {state_deleted} AND posts_post.deleted_by_id != posts_post.user_id AND 
                posts_post.user_id = profiles_user.id
      """.format(state_deleted=Post.STATE_DELETED)
  </code></pre>
  
  <p>This seems okay in the first place. It lists the correct results for the correct users. But problem begins when I try to search a fan profile in the <code>search</code> section in the django admin panel. 
  <a href="https://i.stack.imgur.com/IUNov.png" rel="nofollow noreferrer">Here you can see the search section and the correct deleted post count lists</a></p>
  
  <p>When I want to search a fan profile,  here what happens :</p>
  
  <pre><code>ProgrammingError at /cp/profiles/fanprofile/
  missing FROM-clause entry for table "profiles_user"
  LINE 5:                   posts_post.user_id = profiles_user.id
                                     ^
  </code></pre>
  
  <p>but I can see that the query works fine when it comes to listing the deleted post counts.</p>
  
  <p>Here is my fanprofile class from admin.py</p>
  
  <pre><code>class FanProfileAdmin(OSMGeoAdmin, BaseUserAdmin):
   model = FanProfile
   actions = ("send_push_notification",)
   fieldsets = (
      (None, {'fields': ('email', 'username', 'password')}),
      (_('Profile'), {'fields': ('full_name', 'avatar', 'year_of_birth', 'gender', 'language', 'about', 'location_name', "abuse_report_validity_score",)}),
      (_('Permissions'), {'fields': ('receive_message', 'is_visible',  'is_profile_visible', 'is_staff', 'is_superuser',
                                     'groups', 'user_permissions')}),
      (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
   )
  
    add_fieldsets = (
      (None, {
          'classes': ('wide',),
          'fields': ('email', 'username', 'password1', 'password2')
      }),
   )
  
   add_form = FanCreationForm
  
   list_display = ('username', 'email', 'is_profile_visible', "abuse_report_validity_score", 'is_staff', 'date_joined', 'deleted_post_count')
   readonly_fields = ("date_joined", "language", "abuse_report_validity_score", "is_visible", "is_profile_visible")
  
   def get_urls(self):
       return self.get_push_notification_urls() + super(FanProfileAdmin, self).get_urls()
  
   def has_add_permission(self, request):
       return False
  
   def suit_row_attributes(self, obj, request):
       if not obj.is_visible:
           return {'class': 'error'}
  
   def delete_model(self, request, obj):
       obj.log_action("Deactivated", log_user_id=request.user.id)
  
      super(FanProfileAdmin, self).delete_model(request, obj)
  
   def get_queryset(self, request):
       queryset = super(FanProfileAdmin, self).get_queryset(request)
  
       sql_deleted_post_count = """
          SELECT COUNT(*) 
          FROM posts_post 
          WHERE posts_post.state = {state_deleted} AND posts_post.deleted_by_id != posts_post.user_id AND 
                posts_post.user_id = profiles_user.id
      """.format(state_deleted=Post.STATE_DELETED)
       return queryset.annotate(total_deleted_post_count=RawSQL(sql_deleted_post_count, []))
  
   def deleted_post_count(self, obj):
      return obj.total_deleted_post_count
  
   deleted_post_count.admin_order_field = "total_deleted_post_count"
  </code></pre>
  
  <p>I don't know why this causes an issue while I try to search a fan profile instead of listing deleted post counts. 
  Could you please help ? </p>
  
  <p><strong>Edit</strong>
  I am using postgre sql. </p>
  
  <p>Thanks.</p>