django-queryset

How to use a related manager and reverse lookups to clean up this Django queryset

一世执手 提交于 2020-01-16 20:19:12
问题 I have some working code, but have recently learned about Related Managers and reverse lookups and would like to know how to apply them to this code: The hacky method I would like to utilize a Related Manager/reverse lookup is get_by_type_for_user(self, user) : class BadgeAssertionQuerySet(models.query.QuerySet): def get_user(self, user): return self.filter(user = user) def get_type(self, badge_type): return self.filter(badge__badge_type = badge_type) ... class BadgeAssertionManager(models

Case insensitive search in django

那年仲夏 提交于 2020-01-16 17:20:27
问题 I'm trying to do a case insensitive search for a substring within a field in my model. My model: class doctor(models.Model): docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary name = models.CharField(max_length=35) username = models.CharField(max_length=15) regid = models.CharField(max_length=15, default="", blank=True) photo = models.CharField( max_length=35, default="", blank=True) email = models.EmailField(default="", blank=True) phone = models

Django nested inlines not working?

耗尽温柔 提交于 2020-01-16 04:48:23
问题 I've installed django-nested-inline but I have problems. Here's my code: from django.contrib import admin from nested_inline.admin import NestedStackedInline, NestedModelAdmin from .models import Positive, Negative, Option, Question class PositiveInline(NestedStackedInline): model = Positive class NegativeInline(NestedStackedInline): model = Negative class OptionInline(NestedStackedInline): model = Option inlines = [PositiveInline, NegativeInline,] class QuestionAdmin(NestedModelAdmin):

Django SearchVector on choices field

扶醉桌前 提交于 2020-01-14 04:51:05
问题 If I have a simple Django model like: from model_utils import Choices class Art(models.Model): CATEGORIES = Choices( (1, 'DIGITAL_ART', 'Digital Art'), (2, 'MULTIMEDIA', 'Multimedia'), ) title = models.TextField() category = models.PositiveSmallIntegerField( db_index=True, choices=CATEGORIES, blank=True, null=True ) How can I use SearchVector in postgres to allow for searching on both the title and categories fields? E.g. so "Some Book Digital Art" would query over both the title and

Translating query with JOIN expressions and a generic relation to Django ORM

无人久伴 提交于 2020-01-14 04:32:06
问题 class Business(models.Model): manager = models.ForeignKey(User, on_delete=models.CASCADE) #... class Event(models.Model): business = models.ForeignKey(Business, on_delete=models.CASCADE) text = models.TextField() when = models.DateTimeField() likes = GenericRelation('Like') class Like(models.Model): person = models.ForeignKey(User, on_delete=models.CASCADE) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type'

Newbie: Django : Adding calculated results to Queryset before passing to template

一世执手 提交于 2020-01-12 15:55:31
问题 Its day two of my new life with Django, please excuse the simplicity of my question. I have an existing DB table(read-only access) that I have successfully displayed the contents of on a webpage using urls, views, models and all that good stuff. The challenge I have is the table does not contain all the information I need to display. The table contains test results with the columns, sampletime, samplevalue, sampleresult. I need to display different data based on what I calculate from those

Django: get unique object list from QuerySet

强颜欢笑 提交于 2020-01-12 06:37:23
问题 I have the following (simplified) models in my Django app: class Color(models.Model): name = models.CharField(max_length=10) class Item(models.Model): name = models.CharField(max_length=200) color = models.ForeignKey(Color, blank=True, null=True) class Favorite(models.Model): user = models.ForeignKey(User) item = models.ForeignKey(Item) I'm currently getting all the items I need using the following query: favorites = Favorite.objects.filter(user=request.user) How can I get all the distinct

Get multiple rows with one query in django?

自古美人都是妖i 提交于 2020-01-11 17:58:41
问题 How can I get build a QuerySet that gets multiple rows from django? I thought filter() would work, but it seems to be worse off. For example, I have two rows in the model Car, with two text attributes (license and vin). Now say I want to print the licenses and vins from these cars. How can I do that with one database call? Here's an answer that will make two database calls: #using get(), two total queries a = Car.objects.get(id=1) #query here b = Car.objects.get(id=2) #query here print(a

How append sum of instances within a django queryset to that queryset?

試著忘記壹切 提交于 2020-01-11 11:32:42
问题 I have a Django Queryset object that looks like this (it is a derived queryset, not a queryset for a Model): <QuerySet [{'A': 2, 'B':3, 'C':0 }, {'A': 1, 'B':2, 'C':1 }]> How can I append a new instance containing sum of instances within the queryset to that queryset? That is, I need to make a new (or the same) queryset that looks like this: <QuerySet [{'A':2, 'B':3, 'C':0 }, {'A':1, 'B':2, 'C':1 }, {'A':3, 'B':5, 'C':1 }]> 回答1: I don't think you can do that. But if you use aggregate then you

Django query with order_by, distinct and limit on Postgresql

痴心易碎 提交于 2020-01-11 04:48:24
问题 I have the following : class Product(models.Model): name = models.CharField(max_length=255) class Action(models.Model): product = models.ForeignKey(Product) created_at = models.DateTimeField(auto_now_add=True) I would like to retrieve the 10 most recent actions ordered by created_at DESC with distinct products. The following is close to the result but still misses the ordering: Action.objects.all().order_by('product_id').distinct('product_id')[:10] 回答1: Your solution seems like it's trying to