django - how to get a queryset based on a count of references to foreign field
Tag : django , By : Matthew Steed
Date : March 29 2020, 07:55 AM
This might help you It's confusing when you say "extract" as in exclude. The answer is in annotation and aggregation. from django.db.models import Count
Games.objects.annotate(num_players=Count('players')).filter(num_players__gt=10)
|
Django: Get row based on maximum value of a foreign key field value
Date : March 29 2020, 07:55 AM
Hope this helps since you want to group by user and category, i think you need something like MyModel.objects.values('user', 'category'
).annotate(max_rank=Max('status__rank'))
|
Filtering list_filter in Django admin based on field in foreign key
Tag : django , By : Jody Bannon
Date : March 29 2020, 07:55 AM
it should still fix some issue If I'm getting it right you're trying to filter Issue instances not by the type of the Organization they belong to, but by the Organization instances themselves, so you will have on the right a potentially very long list of Organizations to filter Issues on, but you want only CHARITies to appear in this list. This may not be the best solution if there are many Organizations, but only if they're few... class CharityFilter(SimpleListFilter):
title = _('Charity')
parameter_name = 'charity'
def lookups(self, request, model_admin):
queryset = model_admin.queryset(request).filter(organisation__type='CHARITY')
return queryset.values_list('organisation__pk','organisation__name').order_by('organisation__name')
def queryset(self, request, queryset):
if self.value():
return queryset.filter(organisation=self.value())
|
In a Django serializer, how to set foreign key field based on view argument?
Date : March 29 2020, 07:55 AM
wish of those help I think you are looking for a HiddenField with a combination of CreateOnlyDefault class DomainDefault(object):
def set_context(self, serializer_field):
view = serializer_field.context['view']
request = serializer_field.context['request']
self.domain = ...#determine the domain based on request+view
def __call__(self):
return self.domain
class RRsetSerializer(serializers.ModelSerializer):
domain = serializers.HiddenField(default=serializers.CreateOnlyDefault(DomainDefault()))
|
How can I populate a select field based on the choice of it's foreign key in Django?
Date : March 29 2020, 07:55 AM
|