Wednesday, November 30, 2016

Django: UpdateView - dispatch() : reduce the number of queries


I have a Product Model, foreign key to Company Model, onetoone key to User.

I override the dispatch(method) to check if the user has rights to edit the object(product).

I'm trying to optimize the queries to the database because some of them are duplicates.

def dispatch(self, request, *args, **kwargs):
    obj
= self.get_object()
   
if obj.company.user != request.user:
       
raise PermissionDenied
   
else:
       
print('ok')
       
return super().dispatch(request, *args, *kwargs)



query to products

obj = self.get_object()



query to company and query to user twice

if obj.company.user != request.user:



query to products again

 return super().dispatch(request, *args, *kwargs)



How can I optimize and remove duplicated queries ?

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/725caa3d-4686-418a-b947-db4be442f0fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

makemigrations generates new migration when nothing has changed

Any idea why makemigrations would fail to recognise that a migration has already been generated?

makemigrations.py is a script which calls makemigrations with appropriate django settings. If I run this once, it will generate the initial migration, but every time I run it subsequently, it will regenerate a migration even though nothing has changed in the model. This is with django 1.10.3 and postgres. The field sk that is constantly regenerated is a foreign key relation to an unmanaged database, but I have referred to this in a different app with no bother.

I am using django 1.10.3.

I found a post in this group that had a similar issue with instantiating a class in the model that was missing an implementation for __eq__ but that doesn't apply in this case as I don't instantiate any class in the model file.

Is there any common mistake that leads to this outcome?

------------------

user@testenv:~/eit/testproj$ ./makemigrations.py 
Migrations for 'sk':
  testproj/sk/migrations/0001_initial.py:
    - Create model SKM
user@testenv:~/eit/testproj$ ./makemigrations.py 
Migrations for 'sk':
  testproj/sk/migrations/0002_auto_20161130_1643.py:
    - Alter field s on skm
user@testenv:~/eit/testproj$ 
user@testenv:~/eit/testproj$ ./makemigrations.py 
Migrations for 'sk':
  testproj/sk/migrations/0003_auto_20161130_1643.py:
    - Alter field s on skm


--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad7c5eb6-4542-456e-8e53-675c02e897b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

GeoDjango with Docker

Hi, I'm starting a project with GeoDjango and I want to use Docker containers to build the stack (GeoDjango, Nginx, Postgis) preferably Alpine base images.
Does anyone has successful done this before? Where can I find a useful docker-compose.yml or a tested and working updated image?
Thanks a lot,
Tadeo

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7c0649e1-e2d8-4e56-9930-14e2f95135ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Django Admin site paginator does not appear

I'm setting up a django paginator in the Django Admin Site in order to display the elements 30by30 on the changelist. To do so, I'm using "list_per_page=30" on the DocumentAdmin.

It works fine on Firefox, but the paginator doesn't appear on Safari, either in iMac, iPad or iPhone... so I can only see the first 30 elements. However, it does appear when I reduce the amount of elements I want to display, for instance, 8. I have similar problems using Chrome.

Am I missing something? Any help is really welcome.

Thanks for your time.

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/41104ca8-def5-491a-8844-8a5ba560b7eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Djago URL conf error

I have the following message when I try to use reverse or reverse_lazy:


The included URLconf 'ph.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import


When I use the urls (without using reverse or reverse lazy) is working with no issue. I tried different combination of urls the same results


What I'm doing wrong ?

ph is the project. In the project I include the urls for the application:

    url(r'^account/', include('accounts.urls', namespace='accounts')),
    url
(r'^companies/', include('companies.urls', namespace='companies')),
    url
(r'^products/', include('products.urls', namespace='products')),
    url
(r'^admin/', admin.site.urls)



The app involved:

1)accounts:

from django.conf.urls import url from .views import AccountDetailView

urlpatterns
= [
     url
(r'$', AccountDetailView.as_view(), name='dashboard'),



2) products:

class ProductCreateView(AccountMixin, CreateView):
    model
= Product
    form_class
= ProductModelForm
    url
= reverse_lazy('accounts:dashboard')
    url2
= reverse_lazy('products:list')
   
#print(url)
    template_name
= 'accounts/product_form.html'
   
#success_url = reverse_lazy('accounts:product_detail')



--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7355569-cd47-4ad8-8f22-68d63a677010%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tuesday, November 29, 2016

Re: Django and asynchronous tasks

Hi All

I spent several days reading and searching examples of how to implement channels. The current status of my project is as follows:
  • I have a page which lets the user enter the data used for the offline calculations, a POST request is send
  • the post request is handled and I can forward these data to a rq-worker, after which I open a basic web-page showing the input data. This page should also hold the results of the asynchronous calculations
  • the rq-worker starts the requested download of data from a site and I store the data in the apps (gnsspredict) directory. The rq-worker stops with a success reported in the log.
  • not yet implemented, but this is similar to the download thread, I shall further launch 2 to 3 workers to split up the work so that I can inform the user of the progress of the async calculations
But this is where I do not know what to do next.

As far as I understand 
  • the rq-worker can send its results using a dictionary to a 'reply-channel', but where does this channel goes to? 
  • of the few complete working examples on github I found I assume that I have to write a javascript (which I never used) to treat the results from the worker thread? I also remark that the javascript has as name 'function()', is this always the case? If so, for all 3 to 4 workers how do I write a script that takes action on which asyn cthread that has finished?
All help is appreciated

Thanks
Alain




On Thursday, 10 November 2016 11:55:01 UTC+1, ludovic coues wrote:
Websocket provide a way for server to send information to the client
without waiting for input from the client.

Django channels [1] is a project to bring native support of websocket
to django. There are alternatives which might involve a bit more of
work

[1] https://channels.readthedocs.io/en/stable/

2016-11-10 11:38 GMT+01:00 Antonis Christofides <ant...@djangodeployment.com>:
> Is there no mechanism that when the background tasks finishes to have a web
> page called which could display the results?
>
> Web pages cannot be "called". They are loaded by the browser. So, what you
> want is a mechanism that notifies the browser that an event has occurred in
> the server. That mechanism is comet.
>
> Antonis Christofides
> http://djangodeployment.com
>
> On 2016-11-10 11:54, Alain Muls wrote:
>
> Hi
>
> Tx for the suggestion but how do I reload a page after eg 30 seconds?
> Is there no mechanism that when the background tasks finishes to have a web
> page called which could display the results?
> I had a look at the signal mechanism of Django but I think that is not
> working since the background task is in another environment than the django
> apps which called it.
>
> bye/alain
>
> On Thursday, 10 November 2016 09:55:08 UTC+1, Antonis Christofides wrote:
>>
>> (Note: The most popular way to do asynchronous tasks is celery, but indeed
>> some
>> people prefer django-rq, which is said to be simpler. But your question is
>> not
>> affected by that.)
>>
>> I'm not an expert but I think that the "correct" way to do what you want
>> would
>> be to use comet (i.e. the opposite of ajax). However, if the work required
>> to
>> make that work is not justified by the budget or the business case, you
>> might be
>> able to get away with a message like "This information is being
>> (re)calculated.
>> Reload the page after half a minute to view the updated results." (That's
>> what I
>> did last time :-)
>>
>> Regards,
>>
>> Antonis
>>
>> http://djangodeployment.com
>>
>> On 2016-11-10 10:04, Alain Muls wrote:
>> > Hi All
>> >
>> > I am building a website which makes calculations about the visibility of
>> > satellites. These calculations take about half a minute so I do not want
>> > to
>> > block the site during this time. I found django-rq and was able to start
>> > a
>> > asynchronous task which handles the calculations.
>> >
>> > The problem I have is how do I find out when the calculations of the
>> > task
>> > thread are done so that I can direct the results to another web page
>> > which
>> > will display them?
>> >
>> > Thanks for your help
>> >
>> > Alain Muls
>> >
>> >
>>
> --
> 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 django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6ae68ef3-6a52-4a59-a87b-3fcd627b4f26%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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 django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0dcd7af0-6833-8174-363a-a68c5b3ff5c9%40djangodeployment.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c7aa0cfa-0371-4a85-a248-8f2f9715aadd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: rewrite PostgreSQL query to Django ORM

I think you would still need some SQL, you could use annotate to add the field with the value of the calculation and then add .order_by() to the queryset


On Tue, Nov 29, 2016 at 11:07 PM, Artem Bernatskyy <artem.bernatskyy@gmail.com> wrote:
Hello, how to rewrite this PostgreSQL query to Django ORM with no raw SQL ?


select * from finance_fund order by((select price from finance_nav where date='2016-11-08' and fund_id=finance_fund.id)/(select price from finance_nav where date='2016-11-07' and fund_id=finance_fund.id)) LIMIT 30;

So basically i need to order by division result of two simple queries of backward relations of Foreign key

Thanks in advance !

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9fbf9c99-c914-437b-aae2-a366a0f8eaec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFWa6t%2BRu7AtgDdmAA36E2G7K80VfxXG7ZFy5n30gDwAtU%3DWxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.