Possible bug in prefetch_related used with Reverse generic relations

2015-02-02 Thread Todor Velichkov
Hi, guys. About a week ago i asked the same question on stackoverflow. Since i got no response there i started to read how to report a bug

Re: SQL select statements to Django ORM

2018-05-13 Thread Todor Velichkov
You can use Raw SQL expressions only for the custom field which you want to annotate. MyModel.objects.all().annotate(age=RawSQL("TIMESTAMPDIFF(YEAR, date_of_birth, curdate( On Sunday, May 13, 2018 at 4:4

Re: Annotate django user queryset with group names

2016-07-22 Thread Todor Velichkov
Hello, I don't think sub-classing `Func` would be appropriate here. A much easier way would be to use `RawSQL ` since you want to dynamically check for different group you could write a factory method to build `

Re: Annotate on related field

2016-07-22 Thread Todor Velichkov
By "retrieving the matching tags" you mean fetching `Tag` objects? Well I guess what you can do is to use `prefetch_related ` with custom `Prefetch

Re: Annotate django user queryset with group names

2016-07-22 Thread Todor Velichkov
ote: >> >> You are awesome! Thank you! >> >> On Friday, 22 July 2016 21:41:58 UTC+1, Todor Velichkov wrote: >>> >>> Hello, I don't think sub-classing `Func` would be appropriate here. A >>> much easier way would be to use `RawSQL >>>

Re: QuerySet for related models

2016-08-04 Thread Todor Velichkov
> > My field hits=models.ForeignKey(Hitcount) means that my Product model has > a related model with multiple fields and all the products in Product model > will have one or more hit records. Instance of Product model will save a > hit from end user by session/ip/user etc. Honestly, I don't u

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
gt; object_id_field='object_pk',) >> >> class Meta: >> ordering = ["-title"] >> Removed the -hits from ordering section and rest of the code is same. I >> searched it on google but it showed some Django based bugs. >> Any suggesti

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
ou both rock and I've learned a > lot with your directions. Still if you have ideas please let me know I will > try those too. > > Regards, > Mudassar > > On Fri, Aug 5, 2016 at 1:04 AM, Todor Velichkov > wrote: > >> Constantine Covtushenko, >> >

Re: QuerySet for related models

2016-08-05 Thread Todor Velichkov
Honestly, now Idea why you get this error, can you paste some more code? And maybe some stack trace? On Friday, August 5, 2016 at 2:20:25 PM UTC+3, M Hashmi wrote: > > 1.8.13 as its LTS. > > On Fri, Aug 5, 2016 at 3:54 AM, Todor Velichkov > wrote: > >> Whats your Django

Re: Updating kwargs['data'] in get_form_kwargs (sometimes) raises AttributeError

2016-08-06 Thread Todor Velichkov
Show some code how you create these forms. Django creates QueryDict objects for request.POST and request.GET which are *immutable*, so mu guess is that some times you are instancing the form with either request.POST

Re: DoesNotExist on ForeignKey Access

2016-08-12 Thread Todor Velichkov
Some more code will be helpful, for example the model structure, how do you define the the `ForeignKey`? How do you subscribe for the signal? You are saying that with sleep(1) the code works fine, is this mean that you can reproduce the problem at any time? However test it without fetching the bl

Re: DoesNotExist on ForeignKey Access

2016-08-12 Thread Todor Velichkov
is not > raised. > > On Fri, Aug 12, 2016 at 11:10 PM, TheBeardedTemplar > wrote: > >> >> >> On Friday, August 12, 2016 at 1:19:17 PM UTC-6, Todor Velichkov wrote: >>> >>> Some more code will be helpful, for example the model structure, how do >>> y

Re: Can't make Django ORM work for multiple models

2016-08-17 Thread Todor Velichkov
> > I only intend to show products added by the shop owner. > You can do this by using the reverse m2m relation between product and shop. products = Product.objects.filter(shop__shop_user=request.user) However, I think it's more suitable for the product to keep the M2M relation, because natur

Re: Can't make Django ORM work for multiple models

2016-08-17 Thread Todor Velichkov
I don't know how much you are into Django, but in case you didn't you really need to ready carefully the following articles from the docs. - Working with forms - Creating forms from models

Re: Python dictionaries saved to models

2016-08-17 Thread Todor Velichkov
Maybe a data migration is what you are looking for. On Thursday, August 18, 2016 at 8:38:33 AM UTC+3, Aaron Weisberg wrote: > > Great- thanks Tim. > > I understand the Python, but what I guess where I'm fuzzy is where to

Re: Django query not returning all objects

2016-08-17 Thread Todor Velichkov
Another solution would be to annotate min/max deadline date and order by the annotation. Something like: cases = HeartFlowCase.objects.all().annotate(min_deadline=Min( 'data__deadline')).order_by('min_deadline') On Thursday, August 18, 2016 at 7:59:19 AM UTC+3, Constantine Covtushenko wrote: >

Re: filter chaining v/s filter with multiple arguments.

2017-04-03 Thread Todor Velichkov
The first one will use a single JOIN on the entry table, and will apply both filters to that table. The second one will JOIN the entry table twice, and for every join will apply only a single filter On Sunday, April 2, 2017 at 5:41:29 PM UTC+3, Mahendra Gaur wrote: > > Hello everyone, > > I am n

Re: filter chaining v/s filter with multiple arguments.

2017-04-03 Thread Todor Velichkov
er. We > are filtering the Blog items with each filter statement, not the Entry > items. Sometimes we need several JOINs over the same table, sometimes we don't. And this is the way to achieve it. You want a single JOIN, put everything into a single .filter call, if you want multipl

Re: filter chaining v/s filter with multiple arguments.

2017-04-04 Thread Todor Velichkov
nt qs2.query On Tuesday, April 4, 2017 at 2:03:45 PM UTC+3, Mahendra Gaur wrote: > > Thanks Todor Velichkov. > > It would be great if you let me know how did you get these queries (tool > name etc). > Because when I was trying to get MySQL query using *q.query*, thes

Re: Apply default filters in admin

2017-04-24 Thread Todor Velichkov
I think a simple redirect can fix your problem. Something like this: if 'lang' not in request.GET: q = request.GET.copy() q.setdefault('lang', 'en') return redirect('%s?%s' % (request.path, q.urlencode())) On Monday, April 24, 2017 at 7:12:18 PM UTC+3, Nate Granatir wrote: > > In

Re: Apply default filters in admin

2017-04-26 Thread Todor Velichkov
) On Tuesday, April 25, 2017 at 2:08:33 AM UTC+3, Nate Granatir wrote: > > Where would I put that code? Is there a way to do it when the Admin form > is initialized? > > On Monday, April 24, 2017 at 5:31:18 PM UTC-5, Todor Velichkov wrote: >> >> I think a simple

Re: {{ form }} vs {{ form }}

2017-05-04 Thread Todor Velichkov
Take a look at: 1) Form rendering options 2) Outputting forms as HTML {{ form }} and {{ form.as_table }} are basically the same. So in te

Re: Aggregating a query while selecting more than grouped by ...

2017-05-13 Thread Todor Velichkov
This is a typical *greatest-per-group* problem, there is even a tag for that in SO: And if you search for questions in this area related to Django you will

Re: Aggregating a query while selecting more than grouped by ...

2017-05-14 Thread Todor Velichkov
[:1] >) >) >) > > But yes I concur this strikes me as a fairly ordinary sort of SQL query in > my experience and hence would be nice if it were well documented how to > generate it in the ORM and I'm pleased you gave it a name > (greatest-pe

Re: Building correct queryset

2017-05-22 Thread Todor Velichkov
Hello, Дмитрий, you can try this one, but w/o further optimizations it may be a very slow query. qs = Product.objects.filter( #Where score is greater or equal #to the 4th max score from its group score__gte=Subquery( (Product.objects .filter(brand=OuterRef('brand')

Is this a bug on annotate+filter+annotate over M2M field.

2017-06-07 Thread Todor Velichkov
Hello, everyone, I just want to get some more feedback on this, before posting it into the tracker. So according to the docs , the order of *annotate* and *filter* matters, since they are not c

Re: request.GET as an OrderedDict

2017-06-08 Thread Todor Velichkov
Did you consider just trying something like this: from django.http import QueryDict from collections import OrderedDict class OrderedQueryDict(QueryDict, OrderedDict): pass my_dict = OrderedQueryDict(request.META['QUERY_STRING']) print request.META['QUERY_STRING']

Is this a bug on annotate+filter+annotate over M2M field.

2017-06-11 Thread Todor Velichkov
I believe issue #28297 is related to the problem described above. https://code.djangoproject.com/ticket/28297 -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to dj

How to get a list of queryset selected fields

2017-06-15 Thread Todor Velichkov
That's it, I want to get a list of all fields which are going to be selected by a queryset, is it possible? e.g. qs = MyModel.objects.only('id', 'name') select = qs.query.select #should be ['id','name'] PS. I notice there is a `qs.query.select`, but its empty. No idea if its the same as what

RE: How to get a list of queryset selected fields

2017-06-15 Thread Todor Velichkov
Hello, Matthew Pava, thank you for your answer. This is probably the solution which I'm going to use if there isn't anything better, but overall I don't like this approach for a few reasons: 1) Dict keys are unordered, I would prefer to be ordered as requested (or as defined in the Model) I gu

Re: How to get a list of queryset selected fields

2017-06-15 Thread Todor Velichkov
Hi, Bruno Soares, well values_list is not what I'm looking for, I'm searching for a list of field names, not field values. Imagine you have a queryset you know nothing about it (how it gets generated) and you want to build a representation (table) of it. The first thing would be to render the

Re: How to get a list of queryset selected fields

2017-06-15 Thread Todor Velichkov
ts.values().first() > which will return None if the queryset is empty, which doesn't have an > attribute of .keys(). > > > -Original Message- > From: django...@googlegroups.com [mailto: > django...@googlegroups.com ] On Behalf Of Todor Velichkov > Sent:

Re: Unexpected behavior on delete of model

2018-11-27 Thread Todor Velichkov
Right now, your doctor will be deleted if its ProfilePicture gets deleted. I'm pretty sure you don't want to do that. I believe this is what you are looking for: class Doctor(models.Model): # When the profile picture gets delete, clear doctors profile picture (set to NULL) pr

Re: objects.filter

2018-11-30 Thread Todor Velichkov
It would be logbook_noc.objects.filter(StatusProgress__in = ['1 Pending', '2 On Progress']) You can read more about this at Making queries and please take a look at Coding styles

Re: Django ORM queryset substring on a column

2019-01-04 Thread Todor Velichkov
You said you cannot use `endswith` because `-A111` and `-111` will match, well what about just use endswith='-111' ? On Thursday, January 3, 2019 at 3:17:43 PM UTC+2, BIJAL MANIAR wrote: > > > Hello, > > Consider below column in mysql table Employee. I need to write django orm > query for below

Re: Filter by sum of filtered related objects?

2019-01-04 Thread Todor Velichkov
Maybe something like this should work: Terms.objects.annotate( transactions_sum=models.Subquery( Transaction.objects.filter( terms=models.OuterRef("pk"), ) # Now In order to filter by some date_range # which vary by timezone, you would want to