﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29551	My use-case of QuerySet.extra(): Join two models without explicit relation	Bowser	nobody	"Hello,
I'm reporting here, as suggested in the docs, my use-case of QuerySet.extra() method, looking if there is any way to make it in a different way.

My django app is inter-operating with an independent third-party email program which created and filled independently some tables in a separate DB.
In order to obtain data from that independent tables in my django system I needed to map those tables in a new django model using the command inspectdb of manage.py. This worked very well with minimal corrections and let me the opportunity to get successfully the data from that tables in my Django system.

My need of QuerySet.extra() came out when I needed to join that special model with other (regular) models of my django system with a field (the email address the sender used) that can be put in relation with some of the customers of my system (but of course it may not be too if there is no relation if the sender email is unknown)
Obviuosly I counldn't put a normal ForeignKey on that model nor I can change the structure of the tables because it could block my third party email system from working properly so I couldn't put a direct regular relation between those models.

This ended in the insertion of the .extra() method in the main queryset of the ModelAdmin (I'm testing that system in the admin panels for now and is working properly with the target of using it in my main app too):

{{{#!python
def get_queryset(self, request):
      return super(InboxAdmin,self).get_queryset(request).extra(
           select={'customer': 'customer_id'},
           where=[""SenderEmail = email""],
           tables=['Customers_customer']
      )
}}}
This is working well but I wonder if this can be done in a better way.

So my question practically is if there is any way in Django to join two models even when there is no explicit relation between them (OneToOne, ManyToMany or ForeignKey) that I can't insert.

Any idea?
Thank you"	Uncategorized	closed	Uncategorized	2.0	Normal	invalid	QuerySet.extra		Unreviewed	0	0	0	0	0	0
