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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:22:03+00:00 2026-06-01T08:22:03+00:00

I’m writing an app where I need to associate data with user pairs. For

  • 0

I’m writing an app where I need to associate data with user pairs. For instance, each user pair will have a compatibility score associated with them, as well as many-to-many relationships such as artists that they have in common. I’m confused about the best way to do this, it seems like I would use a combination of 1) extending User via the one-to-one relationship, 2) using a recursive relationship to self on the User table, 3) coupled with specifying extra fields on M2M relationships, but I can’t wrap my head around what the model would look like.

This is how I am accomplishing this currently, which I assume is not the best way to do it as it requires two passes through the DB for each query:

in models.py (psuedo-code, assume there is an Artist class):

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    zipcode = models.CharField(max_length=16)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)         

class Score(models.Model):
    user = models.ForeignKey(User, related_name='score_first_user')
    second_user = models.ForeignKey(User, related_name='score_second_user')
    dh_score = models.DecimalField(decimal_places=2, max_digits=5)
    cre_date = models.DateTimeField(auto_now_add=True)
    upd_date = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField()

    class Meta:
        unique_together = ('user', 'second_user')   

class UserArtist(models.Model):
    user = models.ForeignKey(User, related_name='userartist_first_user')
    second_user = models.ForeignKey(User, related_name='userartist_second_user')
    artist = models.ForeignKey(Artist)
    cre_date = models.DateTimeField(auto_now_add=True)
    upd_date = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField()

then in views.py I save scores and common artists using something like (pseudo-code):

s = Score(user=u, second_user=second_user score=dh_score)
s.save()

and retrieve them using something like:

u = User.objects.get(username="%s" % username)
user_scores = Score.objects.filter( Q(user=u.id) | Q(second_user=u.id) ).order_by('-dh_score')[:10]

for user_score in user_scores:
# non-relevant logic to determine who is user and who is partner
...

    partner_artists = UserArtist.objects.filter( (Q(user=u.id) & Q(second_user=partner.id))\
                                           | (Q(user=partner.id) & Q(second_user=u.id))
)

What is the best way to accomplish this?

  • 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-06-01T08:22:05+00:00Added an answer on June 1, 2026 at 8:22 am

    Here is how I accomplished the user-to-user data pairing, as well as making a M2M relationship to the intermediate table:

    models.py

    from django.db import models
    from django.contrib.auth.models import User
    from django.db.models.signals import post_save
    
    class UserProfile(models.Model):
        user = models.OneToOneField(User)
        pair = models.ManyToManyField('self', through='PairData', symmetrical=False)
    
    
        def __unicode__(self):
            return "%s's profile" % self.user
    
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            profile, created = UserProfile.objects.get_or_create(user=instance)
    
    post_save.connect(create_user_profile, sender=User)
    
    class PairData(models.Model):
        first_user = models.ForeignKey(UserProfile, related_name='first_user')
        second_user = models.ForeignKey(UserProfile, related_name='second_user')
        raw_score = models.DecimalField(decimal_places=4, max_digits=9)
        dh_score = models.DecimalField(decimal_places=2, max_digits=5)
        distance = models.PositiveIntegerField()
        cre_date = models.DateTimeField(auto_now_add=True)
    
        def __unicode__(self):
            return u"%s %s %f %d" % (self.first_user, self.second_user, self.dh_score, self.distance)   
    
    class Artist(models.Model):
        pair = models.ManyToManyField(PairData)
        artist_name = models.CharField(max_length=256)
    
        def __unicode__(self):
            return u"%s" % self.artist_name
    

    Here is an example of how I queried the pair data (views.py):

    def matches(request, username):
        user_profile = User.objects.get(username=username).get_profile()
        pd = PairData.objects.filter( Q(first_user=user_profile) | Q(second_user=user_profile) ).order_by('-dh_score')
    

    and the artists associated with each pair:

    def user_profile(request, username):
        user_profile = User.objects.get(username=username).get_profile()
        viewers_profile = request.user.get_profile()
    
        pair = PairData.objects.filter( (Q(first_user=user_profile)    & Q(second_user=viewers_profile)) \
                                      | (Q(first_user=viewers_profile) & Q(second_user=user_profile)) )
    
        artists = Artist.objects.filter(pair=pair)
    

    If there is a better way to query without using Q, please share!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.