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 6671639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:24:45+00:00 2026-05-26T03:24:45+00:00

I have a profile. class UserProfile(models.Model): user = models.ForeignKey(User, related_name=%(class)s, unique=True) providers = models.ManyToManyField(ServiceProvider,

  • 0

I have a profile.

class UserProfile(models.Model):
    user = models.ForeignKey(User, related_name="%(class)s", unique=True)
    providers = models.ManyToManyField(ServiceProvider, related_name="%(class)s")

class ServiceProvider(models.Model):
    name = models.CharField(max_length=200, null=False, default="ims")  

How do I get to the user object with just having the profile object.
Can I do: (assuming I have my service provider object)

provider.userprofile.user.get() // or something like that.

I’d like to do that in one sql query. So if I had just the pk of the provider, it would be great to get to the user profile and/or the user that holds that provider.

  • 1 1 Answer
  • 3 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-26T03:24:45+00:00Added an answer on May 26, 2026 at 3:24 am

    Since your User is just a foreign key, all you need is provider.userprofile.user

    If you don’t want that to incur a second SQL query, you can just use the select_related option when selecting your profile, like:

    UserProfile.objects.get(pk=<the_id>,select_related=True)
    

    As written, your models don’t provide a trivial way to get from the provider to the User, since there is a ManyToMany from the ServiceProvider to the UserProfile. You’ll have to get the set of UserProfiles associated with the provider, get the one you want, and then proceed to get the User as above.

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

Sidebar

Related Questions

I have the following models: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True, related_name='profile') starred_authors =
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
15 class Profile(models.Model): 16 17 User profile model 18 19 user = models.ForeignKey(User, unique=True)
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1,
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',

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.