Python django.db.models.aggregates.Min() Examples

The following are code examples for showing how to use django.db.models.aggregates.Min(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: django-google-adwords   Author: alexhayes   File: models.py    MIT License 6 votes vote down vote up
def spend(self, start, finish):
        """
        @param start: the start date the the data is for.
        @param finish: the finish date you want the data for.
        """
        account_first_synced = DailyAccountMetrics.objects.filter(account=self).aggregate(Min('day'))
        first_synced_date = None
        if 'day__min' in account_first_synced:
            first_synced_date = account_first_synced['day__min']

        if not self.account_last_synced or self.account_last_synced < finish or not first_synced_date or first_synced_date > start:
            raise AdwordsDataInconsistencyError('Google Adwords Account %s does not have correct amount of data to calculate the spend between "%s" and "%s"' % (
                self,
                start,
                finish,
            ))

        cost = self.metrics.filter(day__gte=start, day__lte=finish).aggregate(Sum('cost'))['cost__sum']

        if cost is None:
            return 0
        else:
            return cost 
Example 2
Project: i-portalen   Author: I-sektionen   File: admin.py    MIT License 5 votes vote down vote up
def get_queryset(self, request):
        qs = super(BookingsAdmin, self).get_queryset(request)
        return qs.annotate(
            start_date=Min("bookings__date")
        ).order_by("-start_date", "-bookings__slot__start_time") 
Example 3
Project: asso   Author: Diacamma2   File: models.py    GNU General Public License v3.0 5 votes vote down vote up
def get_begin_date(self):
        val = self.period_set.all().aggregate(Min('begin_date'))
        if 'begin_date__min' in val.keys():
            return val['begin_date__min']
        else:
            return None 
Example 4
Project: django-google-adwords   Author: alexhayes   File: models.py    MIT License 5 votes vote down vote up
def is_synced(self, start, finish):
            raise NotImplementedError("DailyAccountMetrics.QuerySet.is_synced() is not implemented.")
            pass
#             account_first_synced = DailyAccountMetrics.objects.filter(account=self).aggregate(Min('day'))
#             first_synced_date = None
#             if account_first_synced.has_key('day__min'):
#                 first_synced_date = account_first_synced['day__min']
#
#             if not self.last_synced or (self.last_synced - timedelta(days=1)) < finish or not first_synced_date or first_synced_date > start: 
Example 5
Project: wifi-attendance   Author: elvinzeng   File: views.py    Apache License 2.0 5 votes vote down vote up
def get(self, request):
        if request.user.is_authenticated():
            histories = OnlineHistory.objects.filter(mac=request.user.username)\
                .values('date').annotate(min_time=Min('time'), max_time=Max('time')).order_by("-date")
            return render(request, "index.html", locals())
        else:
            verification_token = str(uuid.uuid4())
            request.session["verification_token"] = verification_token
            return render(request, "authentication.html", locals()) 
Example 6
Project: wifi-attendance   Author: elvinzeng   File: views.py    Apache License 2.0 5 votes vote down vote up
def get(self, request):
        if request.user.has_perm("mobile_scanner.view_staffonlinehistory"):
            histories = OnlineHistory.objects.filter()\
                .values('date', 'user__last_name', 'user__first_name').annotate(min_time=Min('time'), max_time=Max('time')).order_by("-date")
            return render(request, "staff.html", locals())
        else:
            msg = "无访问权限"
            return render(request, "msg.html", locals()) 
Example 7
Project: autostew   Author: Autostew   File: models.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_fastest_laps_by_vehicle(self, vehicle):
        return Lap.objects.filter(
            session__setup_actual__track=self,
            participant__vehicle=vehicle,
            count_this_lap=True,
            participant__is_player=True,
        ).values(
            'participant',
            'participant__member__steam_user__display_name',
            'participant__vehicle__name'
        ).annotate(fastest_lap_time=Min('lap_time')).order_by('fastest_lap_time') 
Example 8
Project: autostew   Author: Autostew   File: models.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_fastest_laps_by_vehicle_class(self, vehicle_class):
        return Lap.objects.filter(
            session__setup_actual__track=self,
            participant__vehicle__vehicle_class=vehicle_class,
            count_this_lap=True,
            participant__is_player=True,
        ).values(
            'participant__name',
            'participant__vehicle__name'
        ).annotate(fastest_lap_time=Min('lap_time')).order_by('fastest_lap_time') 
Example 9
Project: autostew   Author: Autostew   File: models.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _best_in_stage_evaluation(self, field_name):
        return getattr(self, field_name) <= self.participant.lap_set.filter(
            **{
                '{}__gt'.format(field_name): 0,
                'session_stage': self.session_stage
            }
        ).aggregate(Min(field_name))['{}__min'.format(field_name)]