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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:49:34+00:00 2026-05-27T19:49:34+00:00

I have a model Entry class Entry(db.Model): year = db.StringProperty() . . . and

  • 0

I have a model Entry

class Entry(db.Model):
    year = db.StringProperty()
    .
    .
    .

and for whatever reason the last name field is stored in a different model LastName:

class LastName(db.Model):
    entry = db.ReferenceProperty(Entry, collection_name='last_names')
    last_name = db.StringProperty()

If I query Entry and sort it by year (or any other property) using .order() how would I then sort that by the last name? I’m new to python but coming from Java I would guess there’s some kind of comparator equivalent; or I’m completely wrong and there’s another way to do it. I for sure cannot change my model at this point in time, though that may be the solution later. Any suggestions?

EDIT: I’m currently paginating through the results using offsets (moving to cursors soon, but I think it would be the same issue). So if I try to sort outside of the datastore I would only be sorting the current set; it’s possible that the first page will be all ‘B’s and the second page will have ‘A’s, so it will only be sorted by page not by overall set. Am I screwed the way my models are currently set up?

  • 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-27T19:49:34+00:00Added an answer on May 27, 2026 at 7:49 pm

    A few issues here.

    There’s no way to do this sorting directly in the datastore API, either in Python or Java – as you no doubt know, the datastore is non-relational, and indirect lookups like this aren’t supported.

    If this was just a straight one-to-one relationship, which gave you an accessor from the Entry entity to the LastName one, you could use the standard Python sort function to sort the list:

    entries.sort(key=lambda e: e.last_name.last_name)
    

    (note that this sorts the list in place but returns None, so don’t try assigning from it).

    However, this won’t work, because what you’ve actually got here is a one-to-many relationship: there are potentially many LastNames for each Entry. The definition actually recognises this: the collection_name attribute, which defines the accessor from Entry to LastName, is called last_names, ie plural.

    So what you’re asking doesn’t really make sense: which of the potentially many LastNames do you want to sort on? You can certainly do it the other way round – given a query of LastNames, sort by entry year – but given your current structure there’s not really any way of doing it.

    I must say though, although I don’t know the rest of your models, I suspect you have actually got that relationship the wrong way round: the ReferenceProperty should probably live on Entry pointing to LastName rather than the other way round as it is now. Then it would simply be the sort call I gave above.

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

Sidebar

Related Questions

I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =
Consider I have defined the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model):
I have the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model): title =
I have two models class Status(models.Model) name = models.CharField(max_length=25) rank = models.PositiveSmallIntegerField() class MyModel(models.Model)
I have a model which has a field status definde as: class Model(models.Model): ...
For the following models: class Entity(models.Model): name = models.CharField(max_length=256) class Entry(models.Model): A <subj> has
I have models like this class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def
I have the following routing configuration: entry: class: sfDoctrineRouteCollection options: model: ProjectEntry module: entry
I have 2 models which look like that: class Entry(models.Model): user = models.ForeignKey(User) dataname
I have an articles entry model and I have an excerpt and description field.

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.