There are two models:
class AuthUser(models.Model): id = models.IntegerField(primary_key=True) # AutoField? password = models.CharField(max_length=128) last_login = models.DateTimeField(blank=True, null=True) is_superuser = models.BooleanField() first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.CharField(max_length=254) is_staff = models.BooleanField() is_active = models.BooleanField() date_joined = models.DateTimeField() username = models.CharField(unique=True, max_length=30) class Meta: managed = False db_table = 'auth_user' class SocialAuthUsersocialauth(models.Model): id = models.IntegerField(primary_key=True) # AutoField? provider = models.CharField(max_length=32) uid = models.CharField(max_length=255) user = models.ForeignKey(AuthUser, models.DO_NOTHING) extra_data = models.TextField() class Meta: managed = False db_table = 'social_auth_usersocialauth' unique_together = (('provider', 'uid'),)
I need to make an ordinary SQL JOIN and get the contents of these tables in one query by foreground key.
I tried to do this:
def namedtuplefetchall(cursor): desc = cursor.description nt_result = namedtuple('Result', [col[0] for col in desc]) return [nt_result(*row) for row in cursor.fetchall()] def examp(request): context = dict() cursor = connection.cursor() cursor.execute(""" SELECT * FROM auth_user JOIN social_auth_usersocialauth ON auth_user.id = social_auth_usersocialauth.user_id """) context['usersauth'] = namedtuplefetchall(cursor) return render(request, 'try.html', context)
But an error appears: Encountered duplicate field name: 'id', which I can't figure out how to get around. It may be possible to do this somehow using django, but I did not find how.