class Item(models.Model):
name = models.CharField(max_length=255)
user = models.ForeignKey(User)
class Document(models.Model):
doc_type = models.CharField(max_length=10, default="DOC")
item = models.ForeignKey(Item, related_name="docs")
uploaded_at = models.DateTimeField(auto_now_add=True)
@api_view(["GET"])
def get_items(request):
# docs__uploaded_at should be from objects having doc_type="DOC" only
# doc = Document.objects.filter(item=item, doc_type="DOC")
items = Item.objects.prefetch_related("docs").filter(user=request.user).order_by("docs__uploaded_at")
Here I want to order items queryset based on document uploaded_at field having doc_type="DOC" only.
You could try like the following.