django-queryset

How to use Django QuerySet.union() in ModelAdmin.formfield_for_manytomany()?

两盒软妹~` 提交于 2021-02-05 06:10:14
问题 Not sure what I am doing wrong here: I tried to use QuerySet.union(), in Django 2.2.10, to combine two querysets (for the same model) inside ModelAdmin.formfield_for_manytomany() . However, when the form is saved, the entire queryset is selected, regardless of the actual selection made. Please consider the minimal example below, based on the standard Django Article/Publication example. from django.db import models from django.contrib import admin class Publication(models.Model): pass class

Django admin tabular inline lookup select dropdown box for a very large queryset

时光毁灭记忆、已成空白 提交于 2021-01-29 15:25:43
问题 I have a django admin tabular inline, in which I have form = ProdForm which holds a modelchoicefield select box as follows; class ProdForm(forms.ModelForm): productid = forms.ModelChoiceField(queryset=Product.objects.filter(active=True), widget=Select2(select2attrs={"width": "400px"}), ) as you can see, I am using the easy_select2 module, that is enabling providing me with a look up field too. However, if I try to load this in the corresponding tabularInLine, it never loads because there is a

Django foreign key query, why it returns None?

巧了我就是萌 提交于 2021-01-28 05:23:13
问题 When I try to query foreign keys using get(), I always get None values, even though I know they are set to 1 in the DB. Am I missing something here? Should I do something different to get the foreignkey values? Here's what the code looks like: class Car_to_brand( models.Model ): brand_id = models.ForeignKey( Brand, db_column='brand_id' ) ... class Brand( models.Model ): (id is not defined here but it's the primary key) ... print Car_to_brand.__dict__.get( brand_id ) That would give me {brand

Django + PostgreSQL: Fill missing dates in a range

℡╲_俬逩灬. 提交于 2021-01-27 05:31:42
问题 I have a table with one of the columns as date . It can have multiple entries for each date. date ..... ----------- ----- 2015-07-20 .. 2015-07-20 .. 2015-07-23 .. 2015-07-24 .. I would like to get data in the following form using Django ORM with PostgreSQL as database backend: date count(date) ----------- ----------- 2015-07-20 2 2015-07-21 0 (missing after aggregation) 2015-07-22 0 (missing after aggregation) 2015-07-23 1 2015-07-24 1 Corresponding PostgreSQL Query: WITH RECURSIVE date_view

Django + PostgreSQL: Fill missing dates in a range

老子叫甜甜 提交于 2021-01-27 05:30:56
问题 I have a table with one of the columns as date . It can have multiple entries for each date. date ..... ----------- ----- 2015-07-20 .. 2015-07-20 .. 2015-07-23 .. 2015-07-24 .. I would like to get data in the following form using Django ORM with PostgreSQL as database backend: date count(date) ----------- ----------- 2015-07-20 2 2015-07-21 0 (missing after aggregation) 2015-07-22 0 (missing after aggregation) 2015-07-23 1 2015-07-24 1 Corresponding PostgreSQL Query: WITH RECURSIVE date_view

Annotate with latest related object in Django

徘徊边缘 提交于 2021-01-21 07:01:36
问题 I have a model Conversation and a model Message . The model Message has a foreign key to conversation, a text field, and a date field. How can I list all conversations and for each conversation get the most recent message and the date of the most recent message? I guess it's something like Conversation.objects.annotate(last_message=Max('messages__date')) but it will only give me the latest date. I want last_message to contain both the text of the last message and the date it was created.

Annotate with latest related object in Django

梦想的初衷 提交于 2021-01-21 07:01:06
问题 I have a model Conversation and a model Message . The model Message has a foreign key to conversation, a text field, and a date field. How can I list all conversations and for each conversation get the most recent message and the date of the most recent message? I guess it's something like Conversation.objects.annotate(last_message=Max('messages__date')) but it will only give me the latest date. I want last_message to contain both the text of the last message and the date it was created.

Django Queryset foreign keys

做~自己de王妃 提交于 2021-01-07 03:12:39
问题 I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in user. views.py def get_queryset(self): assetlist = Asset.objects.filter(organisation__employee__user=self.request.user) print(assetlist) return assetlist models.py class Employee(models.Model): name = models.CharField("Employee Name", max_length=50, blank=False) email = models.CharField("Employee

Django forms - create a form for each object in model, and then save to the corresponding PK

回眸只為那壹抹淺笑 提交于 2021-01-07 02:41:46
问题 I'm new to django (and programming in general). Sorry if I used the wrong vocabulary (and please correct me). This is what i'm trying to do: I have this model: Class A(models.Model): name = models.CharField(max_length=100, null=True, default='John') first_number = models.FloatField(blank=True, default=1) second_number = models.FloatField(blank=True, default=2) third_number = models.FloatField(blank=True, default=3) And I have this form: class Numbers(ModelForm): class Meta: model = A fields =

DRF - Paginated response - append list of all ids

一曲冷凌霜 提交于 2021-01-01 20:34:57
问题 I have a "next object" feature on my website but I use pagination. I would like add "ids" field that contains ids of all objects filtered and sorteded into the paginated response. Everything that I tried returns only a list of current page ids. class StandardResultsSetPagination(PageNumberPagination): page_size = 20 page_size_query_param = 'page_size' max_page_size = 20 def get_paginated_response(self, data, list_of_ids): return Response(OrderedDict([ ('count', self.page.paginator.count), (