django-queryset

Django Form If condition in view.py with 2 instance

女生的网名这么多〃 提交于 2020-08-06 05:16:38
问题 TO SAVE DATA that is inputted in form in Django i tried tomake it like this I put this in my model.py class Item(models.Model): CATEGORY = ( ('Gudang Kering', 'Gudang Kering'), ('Gudang Basah','Gudang Basah'), ) name = models.CharField(max_length=200,null= True) stock = models.IntegerField(default='0', blank=False, null=True) category = models.CharField(max_length=200,null= True,choices=CATEGORY) reorderlevel = models.IntegerField(default='0', blank=False, null=True) maxreorderlevel = models

Django Query __isnull=True or = None

纵然是瞬间 提交于 2020-07-31 16:31:29
问题 this is a simple question. I'd like to know if it is the same to write: queryset = Model.objects.filter(field=None) than: queryset = Model.objects.filter(field__isnull=True) I'm using django 1.8 回答1: They are equal: >>> str(Person.objects.filter(age__isnull=True).query) == str(Person.objects.filter(age=None).query) True >>> print(Person.objects.filter(age=None).query) SELECT "person_person"."id", "person_person"."name", "person_person"."yes", "person_person"."age" FROM "person_person" WHERE

usage of iterator() on django queryset

橙三吉。 提交于 2020-07-18 03:31:19
问题 I came across some strange behaviour recently, and need to check my understanding. I'm using a simple filter in the model and then iterating over the results. e.g. allbooks = Book.objects.filter(author='A.A. Milne') for book in allbooks: do_something(book) oddly, it was returning only a partial list of books. However, when using the same code and using iterator(), this seems to work well. i.e. for book in allbooks.iterator(): do_something(book) Any idea why? p.s. I did look through the Django

Annotation to count and return zero when there is no relation

一世执手 提交于 2020-07-15 08:17:02
问题 Given the following relation: class LicenseRequest: license_type = models.ForeignKey(LicenseType) created_at = models.DateField(default=now, editable=False) class LicenseType: name = models.CharField(max_length=100) value = models.CharField(max_length=3, unique=True) I want to count how many requests have been created for each license type. However, since I am generating a graphic, I must include 0 (zero) for license types without any license request in that specific period. I tried to do

Annotation to count and return zero when there is no relation

陌路散爱 提交于 2020-07-15 08:15:36
问题 Given the following relation: class LicenseRequest: license_type = models.ForeignKey(LicenseType) created_at = models.DateField(default=now, editable=False) class LicenseType: name = models.CharField(max_length=100) value = models.CharField(max_length=3, unique=True) I want to count how many requests have been created for each license type. However, since I am generating a graphic, I must include 0 (zero) for license types without any license request in that specific period. I tried to do

Django queryset attach or annotate related object field

余生长醉 提交于 2020-07-04 10:51:42
问题 It is needed to attach to queryset results related object field. Models: class User(models.Model): name = models.CharField(max_length=50) friends = models.ManyToManyField('self', through='Membership', blank=True, null=True, symmetrical=False) class Membership(models.Model): status = models.CharField(choices=SOME_CHOICES, max_length=50) from_user = models.ForeignKey(User, related_name="member_from") to_user = models.ForeignKey(User, related_name="member_to") I can do this: >>> User.objects.all

Django queryset attach or annotate related object field

旧时模样 提交于 2020-07-04 10:51:14
问题 It is needed to attach to queryset results related object field. Models: class User(models.Model): name = models.CharField(max_length=50) friends = models.ManyToManyField('self', through='Membership', blank=True, null=True, symmetrical=False) class Membership(models.Model): status = models.CharField(choices=SOME_CHOICES, max_length=50) from_user = models.ForeignKey(User, related_name="member_from") to_user = models.ForeignKey(User, related_name="member_to") I can do this: >>> User.objects.all

Django: How to filter on inner join based on properties of second table?

喜你入骨 提交于 2020-06-29 11:33:08
问题 My question is simple: in a Django app, I have a table Users and a table StatusUpdates . In the StatusUpdates table, I have a column user which is a foreign key pointing back to Users . How can I do a search expressing something like: users.filter(latest_status_update.text__contains='Hello') Edit: Please excuse my lack of clarity. The query that I would like to make is something like "Give me all the users whose latest status update contains the text 'hello'". In Django code, I would do the

Django template query for loop render taking too much time

孤者浪人 提交于 2020-06-29 05:07:18
问题 I have a related query of limit 10 of an object. It's taking over 9s to load the whole page in development mode. When I'm not running the related query for loop on template, it only takes 1s to load. I'm quite confused what's going on in here! Please help me identify the problem/what I'm doing wrong here! Thanks in advance! Here' my view file code - related = News.objects.filter( is_active=True, status='public', language=request.LANGUAGE_CODE, category=news.category ).order_by('-published_at'

Django template query for loop render taking too much time

你说的曾经没有我的故事 提交于 2020-06-29 05:07:17
问题 I have a related query of limit 10 of an object. It's taking over 9s to load the whole page in development mode. When I'm not running the related query for loop on template, it only takes 1s to load. I'm quite confused what's going on in here! Please help me identify the problem/what I'm doing wrong here! Thanks in advance! Here' my view file code - related = News.objects.filter( is_active=True, status='public', language=request.LANGUAGE_CODE, category=news.category ).order_by('-published_at'