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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:58:37+00:00 2026-05-29T10:58:37+00:00

In a Django UserProfile model, I am trying to do the following: class UserProfile(models.Model):

  • 0

In a Django UserProfile model, I am trying to do the following:

class UserProfile(models.Model):

  ...

  def recent_activity(self):
    followed_users = Followed.objects.filter(user=self.id).order_by("-when")
    ...

    items = [self]
    for f in followed_users:
      items.append(f)

    ...

    sorted(items,key=lambda item: item.getDate)

    return items[:7]

The code is supposed to return a user’s “recent activity” by pulling together a number of different actions they can do and then sorting them by date.

Each model implements a “getDate” method which returns the date to sort by.

The code:

  1. Gets a QuerySet of users that the user has “followed” (I am using a table called Followed to store the fact that user x follows user y).
  2. Creates a list containing the UserProfile object in question.
  3. Add each Followed object to the list.
  4. Sort the list by the last edit date.
  5. Return the most recent n items (7 in the above).

I thought this was a neat way of implementing it. But no matter what I do, Python seems to refuse the correctly sort that list. It seems to sort every type of recent activity correctly except for the UserProfile – which seems to stay wherever I originally put it in that list after calling sorted. It doesn’t fail in any way, it just doesn’t return a correctly sorted list!

I’ve probably missed something simple… but I would expect either Python to complain that an object’s method cannot sort a list that contains the object itself, OR for the list to be correctly sorted. I was a bit surprised that it did neither.

Can anyone tell me where I’ve gone astray?

Thanks.

  • 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-29T10:58:38+00:00Added an answer on May 29, 2026 at 10:58 am

    The issue is that sorted does not sort in-place.

    You have probably read that list.sort() does sort in-place. But sorted() actually returns the sorted list – but you are not assigning it to anything, so it just gets thrown away. Either use sort(), or assign the result to something.

    (Plus, you’ll need to actually call getDate, as Marcin points out.)

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

Sidebar

Related Questions

I have the the following Django model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) full_name
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.OneToOneField(User) score = models.PositiveIntegerField() class
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I've extended Django's contrib.auth.models user like: class UserProfile(models.Model): user = models.ForeignKey(User, related_name = 'profile')
I have django model: class UserProfile(models.Model): user = models.OneToOneField(User) #other fields When I create
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I'm trying to filter the User list in Django using a UserProfile Field... I
I have django models with a manytomany connection with a through class: class userProfile(models.Model):
I am using ModelForm on Django 1.3. models.py: class UserProfile(models.Model): ... gender = models.CharField(max_length=1,

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.