On Oct 15, 5:32 pm, Katja Loeffler <[EMAIL PROTECTED]> wrote: > Hi, > > I am relatively new to Django and I have a small problem making a query > over the following Models ... > > class GeographicTemporalCoverage(models.Model): > beginning_temporal_coverage_date = models.DateField() > ending_temporal_coverage_date = models.DateField() > .... > > class Coverage(models.Model): > geo_temp_coverage = models.ManyToManyField(GeographicTemporalCoverage) > abstract = models.TextField() > ... > > class DataSet(models.Model): > ... > dataset_id_nr = models.CharField(max_length=64) > coverage = models.ForeignKey(Coverage) > ... > > Now I want to query all DataSets having a coverage with a maximal > ending_temporal_coverage of 2008 for example ... > > Can I do it in one query (how) or do I need to make two for that? > > I've tried something like > > ds = > DataSet.objects.select_related().filter(coverage__geo_temp_coverage__ending_temporal_coverage_date<max_year).order_by('dataset_id_nr') > but unfortunately it does not work ;). > ( global name > 'coverage__geo_temp_coverage__ending_temporal_coverage_date' is not defined) > I also did not any hints in the documentation ... > > Thanks in advance! > Katja > > -- > Computer and Database Manager > 3193 Energy, Coast & Environment Bldg. > Louisiana State University > Baton Rouge, LA, 70803
You're on the right track, but you can't use operators like < and > in a filter. You need to use __gt and __lt instead. (Inside the code it's not an expression being evaluated, but a keyword being sent to a function). So your query should be: DataSet.objects.select_related().filter(coverage__geo_temp_coverage__ending_temporal_coverage_date__lt=max_year).order_by('dataset_id_nr') If max_year is actually a year rather than a datetime, you would need to use the __year suffix as well: coverage__geo_temp_coverage__ending_temporal_coverage_date__year__lt=max_year -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---