django-queryset

Django dynamic filter failure

好久不见. 提交于 2019-12-11 15:24:36
问题 As a follow-up to this question, I'd like to pinpoint the actual error that was occurring. Am I doing something wrong, or is this a bug? f = {'groups__isnull': 'True'} students1 = models.Student.objects.filter( **f ) students2 = models.Student.objects.filter(groups__isnull=True) These two queries should be identical, but are not. For reference, my models: class Student (models.Model): user = models.ForeignKey(User, unique=True, null=False, related_name='student') teacher = models.ForeignKey

2 words queryset request in Django for the same variable

♀尐吖头ヾ 提交于 2019-12-11 14:39:23
问题 If the user type "word1" in the form field of var1, I would like to do a query in the database considering the word1 + "word99"(as standard search), how can I do that considering my code bellow? I am new in django. Thanks a lot for the help def qrs_table(request): form = MyForm() query = {} if request.GET.get('Var1'): query['Var1'] = request.GET.get('Var1') if request.GET.get('Var2'): query['Var2'] = request.GET.get('Var2') results = Model.objects.filter(**query).distinct() qrs_table =

filter dataset not having a value

拈花ヽ惹草 提交于 2019-12-11 14:26:46
问题 Suppose i have a schema like below : Book | Author ------------- B1 | A1 B1 | A3 B1 | A2 B2 | A5 B2 | A4 B2 | A3 B3 | A5 B3 | A6 B3 | A1 B4 | A1 B4 | A5 B4 | A6 with below model: class Books(models.Model): author = models.CharField( max_length=100, blank=False, null=False, db_index=True, verbose_name=_('authors'), ) book = models.CharField( max_length=50, blank=False, null=False, db_index=True, verbose_name=_('book'), ) book_type = models.CharField( max_length=50, blank=False, null=False,

django select_for_update and select_related on same query?

六眼飞鱼酱① 提交于 2019-12-11 14:01:14
问题 Does anyone know if you can do both the .select_for_update() and .select_related() statements in a single query? Such as: employee = get_object_or_404(Employee.objects.select_for_update(). select_related(‘company’), pk=3) It seemed to work fine in one place in my code, but a second usage threw an "InternalError: current transaction is aborted" for a series of unit tests. Removing the .select_related and leaving just .select_for_update made the error go away, but I don't know why. I'd like to

How to perform STD Query in Django?

a 夏天 提交于 2019-12-11 09:48:57
问题 How should I perform STD operation when I'm working with Django? SELECT STD(total_cost) FROM purchase; tnx 回答1: If you are looking for the Standard Deviation the following should give you what you want: from django.db.models.aggregates import StdDev print(Purchase.objects.all().aggregate(StdDev('total_cost'))) See the Django documentation for more information on how aggregation works. 来源: https://stackoverflow.com/questions/54184247/how-to-perform-std-query-in-django

connection.queries returns me nothing django

做~自己de王妃 提交于 2019-12-11 09:01:38
问题 from django.db import connection, reset_queries Prints: [] reset_queries() p = XModel.objects.filter(id=id) \ .values('name') \ .annotate(quantity=Count('p_id'))\ .order_by('-quantity') \ .distinct()[:int(count)] print(connection.queries) While this prints: reset_queries() tc = ZModel.objects\ .filter(id=id, stock__gt=0) \ .aggregate(Sum('price')) print(connection.queries) I have changed fields names to keep things simple. (Fields are of parent tables i.e. __ to multiple level) I was trying

Wildcard searching in Django

蓝咒 提交于 2019-12-11 08:57:09
问题 How can we do a wildcard searching in Django. If i am filtering username from a list in database, how is it possible to display the filtered data with those exact usernames or part of it.? def filter(request): val3='' if request.GET.has_key('choices'): val2=request.GET.get('choices') if request.GET.has_key('textField'): val3=request.GET.get('textField') if request.POST: val2=request.POST.get('choices') val3=request.POST.get('textField') if val2=='Designation': newData = EmployeeDetails

How can I optimize queries django rest-framework

北城余情 提交于 2019-12-11 08:55:48
问题 I have the following serilizers class AutoSerializer(serializers.ModelSerializer): class Meta: model = Auto fields = ("nombre",) class MarcaSerializer(WritableNestedModelSerializer): autos = AutoSerializer(many=True) class Meta: model = Marca fields = ("codigo", "descripcion", "autos") ModelViewSet class MarcaViewSet(viewsets.ModelViewSet): queryset = Marca.objects.all() serializer_class = MarcaSerializer def list(self, request, *args, **kwargs): queryset = self.queryset serializer = self.get

Dynamic queryset using request.POST data in class-based generic views

余生颓废 提交于 2019-12-11 08:45:30
问题 I'm interested in how to make dynamic queryset using request.POST query dict? When I did that: class ListCv(ListView): queryset = CV.objects.all() template_name = 'jobseek/applicants_resumes_list.html' paginate_by = 5 def get_context_data(self, **kwargs): context = super(ListCv, self).get_context_data(**kwargs) context['main_form'] = FilterCV(self.request.POST or None) return context def get_queryset(self): request = self.request main_form = FilterCV(request.POST or None) if main_form.is

Prefetch object with multiple levels of reverse lookups

可紊 提交于 2019-12-11 07:27:14
问题 I'm on Django 1.7 and have been using the new Prefetch objects which are a great addition. However I seem to be stuck when I need to traverse back more than one relationship. Here is my code: product_types = self.get_queryset().select_related().prefetch_related( 'excise_category__exciseitem_set__unit', Prefetch( 'bevtank_set__package_set__checkout_set', queryset=CheckOut.objects.filter( create_date__lte=end_date, submission__isnull=True, exempt=False), to_attr='checkouts_due' ) ) ... for pt