Ordering by a custom model field in django
Question: I am trying to add an additional custom field to a django model. I have been
having quite a hard time figuring out how to do the following, and **I will be
awarding a 150pt bounty for the first fully correct answer** when it becomes
available (after it is available -- see as a reference [Improving
Python/django view code](http://stackoverflow.com/questions/6245755/improving-
python-django-view-code)).

I have the following model, with a custom def that returns a video count for
each user --

    
    
    class UserProfile(models.Model):
        user = models.ForeignKey(User, unique=True)
        positions = models.ManyToManyField('Position', through ='PositionTimestamp', blank=True)
    
        def count(self):
            from django.db import connection
            cursor = connection.cursor()
            cursor.execute(
            """SELECT (
                SELECT COUNT(*)
                    FROM videos_video v
                    WHERE v.uploaded_by_id = p.id
                    OR EXISTS (
                        SELECT NULL
                        FROM videos_videocredit c
                        WHERE c.video_id = v.id
                        AND c.profile_id = p.id
                    )
                ) AS Total_credits
                FROM userprofile_userprofile p
                WHERE p.id = %d"""%(int(self.pk))
            )
            return int(cursor.fetchone()[0])
    

I want to be able to order by the count, i.e.,
`UserProfile.objects.order_by('count')`. Of course, I can't do that, which is
why I'm asking this question.

Previously, I tried adding a custom model Manager, but the problem with that
was I also need to be able to filter by various criteria of the UserProfile
model: Specifically, I need to be able to do:
`UserProfile.objects.filter(positions=x).order_by('count')`. In addition, I
need to stay in the ORM (cannot have a raw sql output) and I do not want to
put the filtering logic into the SQL, because there are various filters, and
would require several statements.

How exactly would I do this? Thank you.


Answer: My reaction is that you're trying to take a bigger bite than you can chew.
Break it into bite size pieces by giving yourself more primitives to work
with.

You want to create these two pieces separately so you can call on them:

  * Does this user get credit for this video? return boolean
  * For how many videos does this user get credit? return int

Then use a combination of @property, model managers, querysets, and methods
that make it easiest to express what you need.

For example you might attach the "credit" to the video model taking a user
parameter, or the user model taking a video parameter, or a "credit" manager
on users which adds a count of videos for which they have credit.

It's not trivial, but shouldn't be too tricky if you work for it.

