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
2 answers

Getting related objects via tags in Django

My goal is to build a "Recommended Products" section in my e-commerce website when accessing an individual product page. I have a sereis of products which have several user-defined tags in the admin. The tagging system is a combination of…
DDiran
  • 533
  • 1
  • 6
  • 23
2
votes
3 answers

Django: Best way to retrieve object

Lets assume that I have a class Person with a field email. I want to get a single record based on queryset and assign it to variable person. Sadly I cannot do person = Person.objects.filter(email="xxx@xxx.xxx")[0] because I might get an exception.…
Thanos Darkadakis
  • 1,669
  • 2
  • 18
  • 30
2
votes
1 answer

Django: how to use custom manager in get_previous_by_FOO()?

I have a simple model MyModel with a date field named publication_date. I also have a custom manager that filters my model based on this date field. This custom manager is accessible by .published and the default one by .objects. from datetime…
daks
  • 843
  • 2
  • 7
  • 18
2
votes
1 answer

"OperationalError, unrecognized token" while searching

I'm getting this error while making any search query: OperationalError at /search/ unrecognized token: "@" My view: class Search(ListView): model = Opinion template_name = 'home/search.html' context_object_name = 'search_results' …
steerr
  • 151
  • 2
  • 4
  • 13
2
votes
0 answers

Getting latest value in a queryset?

I have a table that looks a bit like this: date | timestamp | predicted_value 2017-10-25| 21758492893 | 0.4 2017-10-25| 21758492917 | 0.3 2017-10-25| 21758493210 | 0.4 2017-10-25| 21758493782 | 0.2 2017-10-25|…
user31415629
  • 925
  • 6
  • 25
2
votes
1 answer

Django Query: Group by field and get the latest entry per group

I have the following table in Django-1.11: class Market(models.Model): slug = models.SlugField(...) active = models.DateTimeField(...) It would be great if the slug was a foreign key to avoid duplicate values but this is not the case, I…
raratiru
  • 8,748
  • 4
  • 73
  • 113
2
votes
1 answer

Django query ForeignKey Count() zero

I have 3 tables: Truck with the fields: id, name.... Menu with the fields: id, itemname, id_foodtype, id_truck... Foodtype with the fields: id, type... I want to get a summary like: id name total 10 Alcoholic drink 0 5 Appetizer …
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
2
votes
2 answers

Format Django QuerySet to output values as an array

I have data in a table that look like this: src_id, dst_id, params int , int , array I'm querying the data to extract some values from the array with the following Django query dataset = query_set.values_list('src_id', 'dst_id', *[e.field for e…
MrE
  • 19,584
  • 12
  • 87
  • 105
2
votes
2 answers

how to get specific list with all data from a queryset django?

on server sends this data: Object { categoryes: Array[2], brands: Array[2], discount_list: "all", category_slug: "accessories", category_of_relationsheep: "m" } if i print request.POST in server side i got this:
InvictusManeoBart
  • 373
  • 3
  • 8
  • 25
2
votes
2 answers

Django annotate Avg on model field

I have these models: class Agency(models.Model): pass class User(models.Model): agency = models.ForeignKey(Agency) class Feedback(models.Model): rating = models.DecimalField() user = models.ForeignKey(User) and I want to…
2
votes
1 answer

How to get a list of tuples from a Django RawQuerySet?

I'm doing a complex query with raw SQL within django to solve some annotation issues. The actual query has many left joins that have been converted to subqueries in order to get around a major bug in…
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
2
votes
2 answers

Django ORM orderby exact / prominent match to be on top

I need to order the results based on the length of match in Django ORM. I have a Suburb table with location details in name field. I have a requirement to search the table with given text and order by exact match / most prominent match to be the…
Seshadri VS
  • 550
  • 1
  • 6
  • 24
2
votes
2 answers

Django - get queryset with id's not in set of values

According to doc - https://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut - I can get set of objects with specified in list ids. Is there any short way to get another set of objects, with id's not in the specified list.…
Naaim Iss
  • 181
  • 1
  • 9
2
votes
2 answers

Django: I want a Queryset as a list. How?

I need to get a Queryset in Django and make a copy of it as a variable in the form of a list of dictionaries. I thought this is what Querysets were, I guess I'm wrong. listA = User_Item.objects.filter(user=User.objects.get(id=1)) Gives me an error…
V. Snow
  • 133
  • 1
  • 14
2
votes
2 answers

In Django, getting next/previous object by date when some share the same date

In Django I have a Letter model: class Letter(models.Model): letter_date = models.DateField(blank=False, null=False) When viewing an individual letter, using a DateDetailView(), I want to show "next" and "previous" links. To get the…
Phil Gyford
  • 13,432
  • 14
  • 81
  • 143
1 2 3
99
100