django-queryset

Mocking a Django Queryset in order to test a function that takes a queryset

早过忘川 提交于 2019-12-18 12:05:50
问题 I have a utility function in my Django project, it takes a queryset, gets some data from it and returns a result. I'd like to write some tests for this function. Is there anyway to 'mock' a QuerySet? I'd like to create an object that doesn't touch the database, and i can provide it with a list of values to use (i.e. some fake rows) and then it'll act just like a queryset, and will allow someone to do field lookups on it/filter/get/all etc. Does anything like this exist already? 回答1: Not that

How to get primary keys of objects created using django bulk_create

依然范特西╮ 提交于 2019-12-18 11:40:07
问题 Is there a way to get the primary keys of the items you have created using the bulk_create feature in django 1.4+? 回答1: 2016 Since Django 1.10 - it's now supported (on Postgres only) here is a link to the doc. >>> list_of_objects = Entry.objects.bulk_create([ ... Entry(headline="Django 2.0 Released"), ... Entry(headline="Django 2.1 Announced"), ... Entry(headline="Breaking: Django is awesome") ... ]) >>> list_of_objects[0].id 1 From the change log: Changed in Django 1.10: Support for setting

QuerySet, Object has no attribute id - Django

人走茶凉 提交于 2019-12-18 11:25:31
问题 I'm trying to fetch the id of certain object in django but I keep getting the following error Exception Value: QuerySet; Object has no attribute id. my function in views.py @csrf_exempt def check_question_answered(request): userID = request.POST['userID'] markerID = request.POST['markerID'] title=request.POST['question'] m = Marker.objects.get(id=markerID) u = App_User.objects.get(id=userID) print userID print markerID print title # userID='1' # markerID='1' # title='Hello' at = AttachedInfo

Simple Subquery with OuterRef

假装没事ソ 提交于 2019-12-18 10:24:29
问题 I am trying to make a very simple Subquery that uses OuterRef (not for practical purposes, just to get it working), but keep running into same error. posts/models.py from django.db import models class Tag(models.Model): name = models.CharField(max_length=120) def __str__(self): return self.name class Post(models.Model): title = models.CharField(max_length=120) tags = models.ManyToManyField(Tag) def __str__(self): return self.title manage.py shell code >>> from django.db.models import OuterRef

Filtering with a Q object on an annotated QuerySet

混江龙づ霸主 提交于 2019-12-18 06:48:55
问题 A mock testcase: def testCount(self): qs = Test.objects.all() qs = qs.annotate(a_count=Count('a_items'), b_count=Count('b_items')) for item in qs: print 'a_count: %d, b_count: %d' % (item.a_count, item.b_count) qs1 = qs.filter(Q(a_count__gt=0)) self.assertEquals(qs1.count(), 1) qs2 = qs.filter(Q(a_count__gt=0) | Q(b_count__gt=0)) self.assertEquals(qs2.count(), 1) Output: a_count: 1, b_count: 0 a_count: 0, b_count: 0 ... FAIL: testCount self.assertEquals(qs2.count(), 1) AssertionError: 0 != 1

Django - How to sort queryset by number of character in a field

爱⌒轻易说出口 提交于 2019-12-18 05:41:29
问题 MyModel: name = models.CharField(max_length=255) I try to sort the queryset. I just think about this: obj = MyModel.objects.all().sort_by(-len(name)) #??? Any idea? 回答1: you might have to sort that in python.. sorted(MyModel.objects.all(),key=lambda o:len(o.name),reverse=True) or I lied ( A quick google search found the following) MyModel.objects.extra(select={'length':'Length(name)'}).order_by('length') 回答2: The new hotness (as of Django 1.8 or so) is Length() from django.db.models.functions

Django - How to sort queryset by number of character in a field

帅比萌擦擦* 提交于 2019-12-18 05:41:13
问题 MyModel: name = models.CharField(max_length=255) I try to sort the queryset. I just think about this: obj = MyModel.objects.all().sort_by(-len(name)) #??? Any idea? 回答1: you might have to sort that in python.. sorted(MyModel.objects.all(),key=lambda o:len(o.name),reverse=True) or I lied ( A quick google search found the following) MyModel.objects.extra(select={'length':'Length(name)'}).order_by('length') 回答2: The new hotness (as of Django 1.8 or so) is Length() from django.db.models.functions

Using dynamic models in Django framework

删除回忆录丶 提交于 2019-12-18 03:35:40
问题 I am currently using Django framework including its Models mechanism to abstract the database schema declaration and general db access, which is working fine for most scenarios. However, my application also requires tables to be created and accessed dynamically during runtime, which as far as I can see, is not supported by Django out of the box. These tables usually have an identical structure, and can basically be abstracted by the same Model class, but Django doesn't let you change the

Whole-word match only in Django query

大城市里の小女人 提交于 2019-12-18 02:42:55
问题 I am trying to write a Django query that will only match whole words. Based on the answer here, I've tried something like: result = Model.objects.filter(text__iregex='\bsomeWord\b') But this isn't returning the expected result. I also tried result = Model.objects.filter(text__iregex=r'\bsomeWord\b') to no avail. My end goal is to be able to pass in a string variable as well, something like: result = Model.objects.filter(text__iregex=r'\b'+stringVariable+r'\b') or result = Model.objects.filter

How do I select from multiple tables in one query with Django?

家住魔仙堡 提交于 2019-12-17 23:08:29
问题 I have two tables, one "Company" and one "Employee": class Company(models.Model): name = models.CharField(max_length=60) class Employee(models.Model): name = models.CharField(max_length=60) company = models.ForeignField(Company) And I want to list every Employee in a table, with the Company next to it. Which is simple enough by calling employees = Employee.objects.all() and in the template loop trough it and calling {{employee.company.name}} . The problem with this solutions is that it will