Questions tagged [django-queryset]

Django querysets are the primary abstraction for retrieving objects from Django's ORM system

Django querysets are the primary abstraction for retrieving objects from Django's ORM system using custom SQL queries. They abstract both the creation of queries as well as the assembly of the model objects that are returned.

See also

6784 questions
2
votes
3 answers

How to get all the related fields from a query set in Django?

I have two models, Session and SessionType which have a many-to-one relationship. There is also a Family foreign key on Session, like so: from django.db import models class SesssionType(models.Model): pass class Session(models.Model): …
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
2
votes
1 answer

Django - return multiple querysets in view

Django 1.10 In my view, I have a function get_queryset() that, currently, returns one queryset. This function is called from another function, get_context(), which takes that data, uses it to get some values, and returns everything to the front…
demluckycharms
  • 421
  • 1
  • 6
  • 17
2
votes
1 answer

python Django ORM group and order a queryset

In my django project i have to implement a groupped query with ORM and order results for a specific field. I do this: a = tt.objects.filter(thread_status = 'DEAD').select_related().distinct('thread_stag').order_by('-id')[0:20] but the response is…
Manuel Santi
  • 1,106
  • 17
  • 46
2
votes
2 answers

Filter data based on sum of two fields

I have following Django model class OfflineExamResult(models.Model): """ Model to store result of offline exam """ batch = models.CharField(max_length=40, null=True, blank=True) date = models.CharField(max_length=20, null=True, blank=True) …
2
votes
1 answer

Prefetch for Django Model properties

I have a problem with optimising my prefetching of a model's properties. But let's start with the code first, that makes it easier to explain my problem: class Tournament(models.Model): name = models.CharField(...) @property def…
2
votes
0 answers

In Django i want to display folders like google drive

This is my models.py. Displaying in folder structure format. class Folder(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='userfolders') folder = models.ForeignKey("self",…
Mike Pinkman
  • 169
  • 9
2
votes
1 answer

Django delete object which has a string as a primary key

I have the following model class Staff(models.Model): username = models.OneToOneField(Person, primary_key=True) room = models.CharField(blank=True, max_length=12) When I run the following code I get the error Truncated incorrect DOUBLE…
John
  • 21,047
  • 43
  • 114
  • 155
2
votes
1 answer

Search by contents of integer field in Django

I have a table containing a list of phone prefixes, stored as integers. However I want to be able to search in those in a 'contains' way, so e.g. when searching for '32' I would like to see all records that contain the actual text '32' in the…
Maarten Ureel
  • 393
  • 4
  • 18
2
votes
2 answers

Django: How to get a distinct Parent List from a Child Queryset related to the User?

These are my models where Apps have menu_items: class App(models.Model): name = models.CharField(max_length=50) class Menu(models.Model): name = models.CharField(max_length=120) app = models.ForeignKey(App, on_delete=models.PROTECT) …
2
votes
1 answer

Django find items from db by values from list?

I have a list of strings: phrases_filter = json.loads(request.GET['phrases_filter']) ['xx', 'yy'] I need to lookup all phrases in db, that contain xx OR yy I tried it like this: Phrase.objects.filter(name__in phrases_filter) But this gave me only…
user2950593
  • 9,233
  • 15
  • 67
  • 131
2
votes
2 answers

Django 1.11: Float in query param in url

TL;DR: how can I accept floats as query params in a url? I have written an API view with Django Rest Framework. The API should be able to list houses by their addresses' coordinates using a GET request. Meaning the user should be able to send a…
2
votes
1 answer

Django 1.11 querysets: Get all objects (model instances) of a relationship set

If have a model for companies. These companies have an attribute employment_set, because workers are assigned to companies via the employment relation. How can I query all employees for a given company? The model for the employements look like…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
2
votes
2 answers

Complex ORM query in django

This is my User class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True, max_length=255) mobile = PhoneNumberField(null=True) username = models.CharField(null=False, unique=True, max_length=255) …
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
2
votes
1 answer

Django orm - how to get distinct items of field within query of distinct dates

I have the following models: class Event(models.Model): date = models.DateTimeField() event_type = models.ForeignKey('EventType') class EventType(models.Model): name = models.CharField(unique=True) I am trying to get a list of all…
roob
  • 2,419
  • 3
  • 29
  • 45
2
votes
1 answer

Construct a django queryset that orders an inner join

I am trying to track page views in a django-based website. I have the following model class PageView: date = DateTimeField( auto_now=True ) user = ForeignKey( User ) page = ForeignKey( Page ) to track each viewing of the page. I want to…
user560494
  • 915
  • 1
  • 9
  • 12