Django: How to use an annotation from a parent queryset? - python

I have an Item class which can be annotated using a custom queryset add_is_favorite_for method:
class ItemQuerySet(QuerySet):
def add_is_favorite_for(self, user):
"""add a boolean to know if the item is favorited by the given user"""
condition = Q(id__in=Item.objects.filter(favoriters=user).values("id"))
return self.annotate(is_favorite=Condition(condition)) # True or False
class Item(Model):
objects = Manager.from_queryset(ItemQuerySet)()
It works as expected. For example:
>>> user = User.objects.get(id=1)
>>> Item.objects.add_is_favorite_for(user) # each item has now a `is_favorite` field
Then, I added a Factory model and link Item model to it using a 1->N relationship:
class Factory(Model):
pass # ...
class Item(Model):
objects = Manager.from_queryset(ItemQuerySet)()
advised_in = models.ForeignKey(
Factory,
on_delete=models.CASCADE,
related_name="advised_items",
)
Now, I'd like to be able to return a Factory QuerySet, whose advised_items fields will all contain the is_favorite annotation too.
I don't know how to do this, I saw no example of such a thing in the doc, maybe I missed it.

You can work with a Prefetch object [Django-doc]:
from django.db.models import Prefetch
Factory.objects.prefetch_related(
Prefetch('advised_items', queryset=Item.objects.add_is_favorite_for(some_user))
)

Related

Django REST Framework - Get reversed value of boolean field in serializer

I have 2 models:
class Model(models.Model):
...
related = models.ForeignKey(
'RelatedModel',
on_delete=models.CASCADE,
related_name='related_model'
)
class RelatedModel(models.Model):
...
flag = models.BooleanField()
I need to pass value of 'flag' attribute of RelatedModel in Model instance serializer and additionally this value must be reversed i.e. if it is 'True', I should return 'False' as boolean data type.
Already implemented this with method:
class ModelSerializer(serializers.ModelSerializer):
...
flag = serializers.SerializerMethodField()
#staticmethod
def get_flag(obj):
return not obj.related.flag
class Meta:
model = Model
fields = (
...
flag
)
But maybe there is opportunity to use only serializer fields like this but with reverse value?
flag = serializers.BooleanField(
source='related.flag', read_only=True
)
If you need to reverse the value you can' t use a BooleanField, the simplest solution is to use a SerializerMethodField as you have already done. Or you could also create a custom field class, but that is more complicated.

Annotate query by existance in M2M

Schema: tables A and B with M2M between them.
I have some queryset QS0 of A objects and one exact instance of B object.
How to annotate QS0 with True if B is connected with A through M2M and False if it is not?
Thanks
Given the example models below
from django.db import models
class ModelA(models.Model):
title = models.CharField(max_length=100)
class ModelB(models.Model):
title = models.CharField(max_length=100)
a_objects = models.ManyToManyField(ModelA, related_name='b_objects')
You should in theory be able to annotate a queryset of ModelA objects on whether they are each linked to a ModelB object in the following way.
from django.db.models import Case, When, Value
b_object = ModelB.objects.get(id=some_id)
QS0 = ModelA.objects.annotate(is_linked_to_b=Case(When(b_objects__id=b_object, then=Value(True)), default=Value(False), output_field=BooleanField())
# QSO[some_index].is_linked_to_b should return either True or False.

General way of filtering by IDs with DRF

Is there a generic way that I can filter by an array of IDs when using DRF?
For example, if I wanted to return all images with the following IDs, I would do this:
/images/?ids=1,2,3,4
My current implementation is to do the following:
# filter
class ProjectImageFilter(django_filters.FilterSet):
"""
Filter on existing fields, or defined query_params with
associated functions
"""
ids = django_filters.MethodFilter(action='id_list')
def id_list(self, queryset, value):
"""
Filter by IDs by passing in a query param of this structure
`?ids=265,263`
"""
id_list = value.split(',')
return queryset.filter(id__in=id_list)
class Meta:
model = ProjectImage
fields = ['ids',]
# viewset
class Images(viewsets.ModelViewSet):
"""
Images associated with a project
"""
serializer_class = ImageSerializer
queryset = ProjectImage.objects.all()
filter_class = ProjectImageFilter
However, in this case ProjectImageFilter requires a model to be specified ( ProjectImage). Is there a way that I can just generally define this filter so I can use it on multiple ViewSets with different models?
One solution without django-filters is to just super() override get_queryset. Here is an example:
class MyViewSet(view.ViewSet):
# your code
def get_queryset(self):
queryset = super(MyViewSet, self).get_queryset()
ids = self.request.query_params.get('ids', None)
if ids:
ids_list = ids.split(',')
queryset = queryset.filter(id__in=ids_list)
return queryset
The library django-filter has support for this using BaseInFilter, in conjunction with DRF.
From their docs:
class NumberRangeFilter(BaseRangeFilter, NumberFilter):
pass
class F(FilterSet):
id__range = NumberRangeFilter(field_name='id', lookup_expr='range')
class Meta:
model = User
User.objects.create(username='alex')
User.objects.create(username='jacob')
User.objects.create(username='aaron')
User.objects.create(username='carl')
# Range: User with IDs between 1 and 3.
f = F({'id__range': '1,3'})
assert len(f.qs) == 3

Using custom methods in filter with django-rest-framework

I would like to filter against query params in my REST API - see django docs on this.
However, one parameter I wish to filter by is only available via a model #property
example models.py:
class Listing(models.Model):
product = models.OneToOneField(Product, related_name='listing')
...
#property
def category(self):
return self.product.assets[0].category.name
Here is the setup for my Listing API in accordance with django-filter docs
class ListingFilter(django_filters.FilterSet):
product = django_filters.CharFilter(name='product__name')
category = django_filters.CharFilter(name='category') #DOES NOT WORK!!
class Meta:
model = Listing
fields = ['product','category']
class ListingList(generics.ListCreateAPIView):
queryset = Listing.objects.all()
serializer_class = ListingSerializer
filter_class = ListingFilter
How can I appropriately filter by listing.category? It is not available on the listing model directly.
Use the 'action' parameter to specify a custom method - see django-filter docs
First define a method that filters a queryset using the value of the category parameter:
def filter_category(queryset, value):
if not value:
return queryset
queryset = ...custom filtering on queryset using 'value'...
return queryset
Listing filter should look like this:
class ListingFilter(django_filters.FilterSet):
...
category = django_filters.CharFilter(action=filter_category)
...
For sake of database speed, you should just add the category to your listing model
class Listing(models.Model):
product = models.OneToOneField(Product, related_name='listing')
category = models.ForeignKey(Category)
Then use a post_save signal to keep the field updated
from django.dispatch import receiver
from django.db.models.signals import post_save
#receiver(post_save, sender=Product)
def updateCategory(sender, instance, created, update_fields, **kwargs):
product = instance
product.listing.category = product.assets[0].category.name
product.listing.save()
Then filter by it's name as you would any other field:
class ListingFilter(django_filters.FilterSet):
...
category = django_filters.CharFilter(name='category__name')
...

Specialized django query

As you can guess from the title, I'm not exactly sure how to describe what I want. Please take a look at the following classes:
from django.db import models
from django.contrib.auth.models import User as Auth_User
class User(Auth_User):
Portfolio = models.ManyToManyField('PortfolioItem', through='SkillTag')
Age = models.IntegerField(blank=False)
#property
def full_name(self):
return self.first_name + ' ' + self.last_name
def __unicode__(self):
return self.full_name
class PortfolioItem(models.Model):
Title = models.CharField(max_length=200, blank=False)
class SkillTag(models.Model):
User = models.ForeignKey('User')
PortfolioItem = models.ForeignKey('PortfolioItem')
Tag_Name = models.CharField(max_length=200, blank=False)
What I need to do, is for every user, get all the Tag_Name values of it's SkillTags, how do I do this?
You can do something like this
class User(Auth_User):
#other attributes
def tag_names(self):
return self.skilltag_set.values_list('Tag_Name', flat=True)
So, here, we are doing a couple of things:
Querying in reverse ForeignKey relationship.
Since you are not using a related_name in the ForeignKey attribute, by default django would assign the model name (lowercase) followed by _set attribute, which makes it .skilltag_set.all()
values_list
Returns a ValuesQuerySet — a QuerySet subclass that returns tuples when used as an iterable, rather than model-instance objects.
Example: [('a'), ('b'), ('c')]
Basically, you are retriving an iterable of ValuesQuerySet (think of it as a list or any other iterables) consisting of tuples.
flat=True
This basically flattens the on-tuples into single values.
Example: ['a', 'b', 'c']
most obvious: using the reverse relationship of ForeignKey fields:
def skill_names_1(user):
return [t.name for t in user.skilltag_set.all()]
The same thing, but explicitly selecting for the user. also, it fetches only the required field from the database.
def skill_names_2(user):
return SkillTag.objects.filter(User=user).values_list('Tag_Name',flat=True)
Either of these can also work as a method of User. Of course, typically the argument would be called self instead of user.
All the skills for a group of users:
def skill_names_3(users):
return SkillTag.objects.filter(User__in=users).values_list('Tag_Name',flat=True)

Categories