Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:01:04+00:00 2026-05-18T03:01:04+00:00

I am currently wanting to run a SQL statement that will filter my database

  • 0

I am currently wanting to run a SQL statement that will filter my database but it accesses multiple tables. From the other examples I have read on this website, they all use foreign keys not on the primary key; however, that is my current setup. I am having two issues, the first being the SQL filter. These are my models:

class UserProfile(models.Model):
        user = models.ForeignKey(User, unique=True);

        isATutor = models.BooleanField();
        timeAvailable = models.CharField(max_length=3);

class TutorProfile(models.Model):
        user = models.ForeignKey(User);

        language = models.CharField(max_length=30);
        unique_together = (("user", "language"), );

class Tutor(models.Model):
        user = models.ForeignKey(User);

        subject = models.CharField(max_length=30);
        unique_together = (("user", "subject"), );

I am using raw SQL at the moment.

def sqlQuery(avail, lang, sub):
    from django.db import connection, transaction
    cursor = connection.cursor();

    cursor.execute("SELECT a.first_name, a.last_name, a.email FROM auth_user a, books_tutor b, books_tutorprofile c, books_userprofile d WHERE a.id = b.user_id AND a.id = c.user_id AND a.id = d.user_id AND b.subject=%s AND c.language=%s AND d.timeAvailable=%s", [sub, lang, avail]);
    row = cursor.fetchall();

    return row;

timeAvailable is taking a three character string is in the form ‘MAE’ where M = morning, A=Afternoon, E=Evening and if they are not needed, we’ll replace with – e.g. ‘M–‘ is only available in morning.

So the first issue is I’d love to be able to keep the above SQL as a django model. The second issue is I need a good way to query for MAE. I’m thinking I may have to do 8 different SQL parts and UNION them depending on what is chosen.

Thanks,

Steve

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:01:05+00:00Added an answer on May 18, 2026 at 3:01 am

    why not use subclassing?

    class UserProfile(User):
        time_available = models.CharField(max_length=3);
    
    class Tutor(UserProfile):
        subject = models.CharField(max_length=30);
    
    class TutorProfile(Tutor):
        language = models.CharField(max_length=30);
        class Meta:
            unique_together = ['username','subject','language'] # <--- username field from User base class.
    
    • UserProfile has access to all User fields, plus time_available.
    • Tutor has access to all User, UserProfile fields, plus subject.
    • TutorProfile has access to all User, UserProfile and Tutor fields, plus language.

    in this way, you can achieve the join simply by looking up an instance of TutorProfile.

    I should mention that when using subclassing, a OneToOneField is automatically created from the subclass to the superclass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
I am wanting to build a validator (javascript) that can validate numbers from different
I wanting to show prices for my products in my online store. I'm currently
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, if I want to output a SQL script for a table in my
Currently I know of only two ways to cache data (I use PHP but
I have Microsoft SQL Server 2008 which we host some databases on. We currently
I'm currently inserting a record into a SQL Server Table and then selecting the
I am currently in the process of debugging a database performance issue. I have
I have 3 home PCs that have 2008 SQL Server Express instances installed on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.