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

Django: M2M field query using 'in' each value in a list

I'm trying to find Problem objects having all the tags contained in tags related to it. I have the following models. class Tag(models.Model): name = models.CharField(verbose_name='Tag Name', max_length=50) description =…
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
2
votes
3 answers

Update Django Object

I've an object:- a = Model.objects.get(id=1) and a dictionary of data attributes of object a. update_data = { colA: 1, colB: 2, colC: 3, } Now, I want to update object a. How can I do it? Note:- .update() works on queryset, not on the object.…
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
2
votes
1 answer

Narrowing down a QuerySet in Django by using difference() and/or exclude()

I'm trying to narrow down a QuerySet in Django. I'm trying to do this by excluding (or taking the difference of) another QuerySet. I've already tried using difference() and exclude() but both of them do not work for some reason. Besides, I’m…
A. S.
  • 81
  • 8
2
votes
1 answer

Django dynamic url. what am i doing wrong?

So I have this URL scheme: (r'^test/(?P\d+)/', 'test'), def test(request, name): html = "it worked" return HttpResponse(html) however, when I go to the following URL, I get a 404 error: http://127.0.0.1:8000/test/words/ What am I…
Zac Altman
  • 1,215
  • 4
  • 25
  • 37
2
votes
2 answers

Django annotate() on two step relationship confusion

I have the following 3 models: Category: date_start date_end active: bool Player: name: str age: int category = models.ForeignKey(Category) PlayerContact: contact_result: int player = models.ForeignKey(Player) In this case I…
Onilol
  • 1,315
  • 16
  • 41
2
votes
2 answers

Django queryset: Exclude all row(s) if any one row with same id is excluded

I am using Django query to filter out some transactions from table where one transaction might have multiple entries in the table. E.g. Sample table +---------------+---------+ | TransactionId | Status | +---------------+---------+ | Txn0 …
kartikmaji
  • 946
  • 7
  • 22
2
votes
1 answer

Django Beginner. How do I update all objects and set a certain field to a value that is a function of another field?

EDIT: I needed a student_count field in course because I'm supposed to use this model for REST API. If you can tell me how to add fields in serializer without adding to model, I'd take that too. This is my model: class Course(models.Model): …
2
votes
1 answer

Django Viewset Pagination throws serialization error

I have something like this: Paginating Viewset: class FeedViewSet(ModelViewSet): queryset = Feed.objects.all() serializer_class = FeedSerializer def list(self, request, *args, **kwargs): paginator =…
2
votes
2 answers

Django - Query using .0. does not work whilst using .first(). creates duplicate queries

I have a need to only get one record from a queryset_set. (as it only returns 1. so I have used .0 or .first() normally. However at the moment when I use .0 I do not get any data. when I use .first() I get duplicated queries (despite the…
AlexW
  • 2,843
  • 12
  • 74
  • 156
2
votes
1 answer

Filtering django REST results by SerializerMethodField

How would I filter results based on a computed field from a Serializer? I tried treating it like any other field, but django doesn't like it. Serializer class ImageSerializer(serializers.ModelSerializer): is_annotated =…
waspinator
  • 6,464
  • 11
  • 52
  • 78
2
votes
1 answer

Getting complex query set with a many to many relationship in django

UPDATE: For clarification sake, I feel like I need to go through the Placement model to get the results I need, because I need the order field from that model. I am trying to get all article objects that are part of a section in an edition that is…
ja408
  • 798
  • 5
  • 16
2
votes
4 answers

Django append for searching each words in a sentece using filter

I want to search each word in a sentence and put that in result['post'] dictionary and of course this code only looks for last query queries = querystring.split() for query in queries: results['posts'] =…
yathomasi
  • 458
  • 1
  • 6
  • 16
2
votes
1 answer

how to merge two different annotate in django?

I have two models like this: class Item(models.Model): title = models.CharField(max_length=128) class Order(models.Model): item = models.ForeignKey(Item) user = models.ForeignKey('users.User',null=True) gift_code =…
nim4n
  • 1,813
  • 3
  • 21
  • 36
2
votes
1 answer

Django paging object has issues with Postgresql QuerySets

I have some django code that runs fine on a SQLite database or on a MySQL database, but it runs into problems with Postgres, and it's making me crazy that no one has has this issue before. I think it may also be related to the way querysets are…
pivotal
  • 736
  • 6
  • 16
2
votes
1 answer

Return all objects that are referenced exactly twice by a related model

I have the following models: class Person(...): name = CharField(...) class Address(...): person = ForeignKey(Person) address = CharField(...) I need to select all persons that have exactly two addresses. So if my Address table looks…
Daniel
  • 3,092
  • 3
  • 32
  • 49