django-queryset

Order Django queryset by value closest to 0

Order Django queryset by value closest to 0 Question: I have a queryset that is returning a list of entries that are tied for the lead in a contest… leaders = ContestEntry.objects.filter(name=game, wins=wins) After I get this data I need to put them in order by a second field (‘difference’) as a tie breaker. The …

Total answers: 2

How to filter an item in one queryset from appearing in another queryset in django

How to filter an item in one queryset from appearing in another queryset in django Question: I have a Django view where I am getting the first queryset of ‘top_posts’ by just fetching all the Posts from database and cutting it in the first four. Within the same view, I want to get "politics_posts" by …

Total answers: 1

DRF ManyToMany Field getting an error when creating object

DRF ManyToMany Field getting an error when creating object Question: I have a Rant model with Category linked to it using ManyToManyField. I’ve serialized it but the problem is this error: { "categories": [ "Expected a list of items but got type "str"." ] } These are my serializers: class CategorySerializer(serializers.ModelSerializer): class Meta: model = …

Total answers: 3

Filter queryset with same DateTime fields

Filter queryset with same DateTime fields Question: I’m trying to filter a queryset with the same DateTime fields: objects = Obj.objects.filter(start_date_time=F(‘end_date_time’)) objects <QuerySet []> models.py: start_date_time = models.DateTimeField(default=now, blank=True) end_date_time = models.DateTimeField(default=now, blank=True) If I remove microseconds, then the fields are equal: obj = Obj.objects.filter(id=’id’).first() obj.start_date_time datetime.datetime(2023, 3, 10, 9, 24, 29, 326238, tzinfo=<UTC>) obj.end_date_time …

Total answers: 2

Try to Return dataset as a table with request of postman

Try to Return dataset as a table with request of postman Question: I wanna Request with postman and get a table of all books as return but I can’t find a way to do this. Thanks a lot This is my model of books: class Book(models.Model): name = models.CharField(max_length=100) username = models.ForeignKey(User, on_delete=models.CASCADE) publication_date = …

Total answers: 1

How to add additional choices to forms which is taken from a model in django

How to add additional choices to forms which is taken from a model in django Question: As in the topic is it possible to add choices to the forms that take from the model. forms.py class BookGenreForm(forms.ModelForm): class Meta: model = Book fields = (‘genre’,) models.py class Book(Product): GENRE_CHOICES = ( (‘Fantasy’, ‘Fantasy’), (‘Sci-Fi’, ‘Sci-Fi’), …

Total answers: 1

How to merge two queryset based on common key name in django

How to merge two queryset based on common key name in django Question: Let’s say I have 2 QuerySet: <Queryset [a, b, c]> and <Queryset [a, d, e]> How can I merge those two to achieve: <Queryset [a, b, c, d, e]> Edit with real case: qs_home = Match.objects.filter(competition=competition) .values(name=F("home_team__name")) .annotate(home_win=Sum(Case(When(home_score__gt=F(‘away_score’), then=1)))) .annotate(home_draw=Sum(Case(When(home_score=F(‘away_score’), then=1)))) .annotate(home_lose=Sum(Case(When(home_score__lt=F(‘away_score’), …

Total answers: 1

How to customise Folium html popups in a for loop in python using .format()?

How to customise Folium html popups in a for loop in python using .format()? Question: I am trying to change the standard popups provided with Folium and make these work with a for loop. I am using Django. I succeeded on change the html, following a few tutorials. However I am struggling to understand how …

Total answers: 1

Removing objects from a queryset by ID optimal implementation

Removing objects from a queryset by ID optimal implementation Question: I have a queryset of 1000000 objects MyModel. All objects in queryset have an ID. I pass a list of ids to remove, for example: ids = [‘1′, ’23’, ‘117’, …] # len = 100000 Then, i need to delete objects in queryset with ids. …

Total answers: 1

Why the first() method and slices works differently in Django?

Why the first() method and slices works differently in Django? Question: I have the model: class PhotoAlbum(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, auto_created=True) name = models.CharField(max_length=50, verbose_name=’Album name’) type = models.ForeignKey(AlbumType, on_delete=models.CASCADE, verbose_name=’Album type’) created_at = models.DateTimeField(auto_now_add=True) And i have this code: print(PhotoAlbum.objects.all().first()) print(PhotoAlbum.objects.all()[:1].get()) it seemed to me that the same objects should be displayed, …

Total answers: 1