Beets API sorting


The API Documentation mentions an optional sort argument, which I assumed was used for specifying how the results should be sorted. However when I tried to used a standard beets sort string I ran into the following:

# offending code
lib.items(query='album_id:1', sort='track+')

# error
File "/home/callum/code/git-repos/beets/beets/", line 1422, in items
  return self._fetch(Item, query, sort or self.get_default_item_sort())
File "/home/callum/code/git-repos/beets/beets/", line 1396, in _fetch
  return super(Library, self)._fetch(
File "/home/callum/code/git-repos/beets/beets/dbcore/", line 1028, in _fetch
  order_by = sort.order_clause()
AttributeError: 'str' object has no attribute 'order_clause'

The funny thing is that including the sort string in the query like lib.items(query='album_id:1 track+') works perfectly, which made me think that there might be a different, special purpose for the sort argument.

Is that the case or am I doing something wrong?

Yo! That parameter actually wants to be a Query object from dbcore:

So for that, you’ll want FieldSort('track').

The funky thing about that function (lib.items) is that the query parameter may be either a string, which gets parsed into both the query and sort parts, or it can be just a Query object, in which case you need to specify the sort separately.

Right! Thanks for clarifying

Just noting for future reference that what’s required is FixedFieldSort('track') rather than just FieldSort('track') as the latter is an abstract class.