开发者

__in Filter Does Not Work

开发者 https://www.devze.com 2023-04-03 12:09 出处:网络
I inherited a Django application that serves high school sports information. I recently received a request to modify a certain set of data so information is displayed only from the current season, whi

I inherited a Django application that serves high school sports information. I recently received a request to modify a certain set of data so information is displayed only from the current season, which I accomplished this way:

teams = Team.objects.filter(season__school_year__in=school_year_filter_list)

(school_year_filter_list contains [1,3])

This query causes an error in pgpool (a Postgres database pooling/replication utility) so I cannot use it. As a side note, the query works properly in the Python shell, and when I bypass pgpool and use Postgres directly. However, our network architecture dictates the use of pgpool, so I am trying to find an alternate way to retrieve the same data.

Can you help me determine another way to get all Teams with a Season in the current SchoolYear?

The (simplified) models look like this:

class Team(models.Model):
    season = models.ForeignKey(Season)

class Season(models.Model):
    school_year = models.ForeignKey(SchoolYear, blank=True, null=True)

class SchoolYear(models.Model):
    school_year = models.CharField(max_length=150)

The '_schoolyear' table looks like:

 id | school_year 
----+-------------
 开发者_如何学编程 1 | 2010-2011
  2 | 2009-2010
  3 | 2011-2012


In the end, I modified another model to make this work. Rather than parsing years to get active seasons, I added an "active" flag to the SchoolYear model and modified my query to check for that flag:

def queryset(self, request):
    qs = super(PlayerYearAdmin, self).queryset(request)
    return qs.filter(team__season__school_year__active=True)


Have you tried
teams = Team.objects.filter(season__school_year__id__in=school_year_filter_list) ? Isn't it right that your school_year_filter_list is a list of school year ids?


Small note: I see 'django-admin' tag, so I think that you add season__school_year_in=[1, 2] to URL query string like /admin/sports/team/?season_school_year__in=[1, 2] Is it correct?

If so, are you shure that school_year_filter_list contains list [1,3], but not a string containing repr of list '[1,3]'

Can you provide us data about error that pgpool or Django ORM returns?

0

精彩评论

暂无评论...
验证码 换一张
取 消