Re: Optimizing admin change view with inlines

2014-12-24 Thread 'Petros Moisiadis' via Django users
On 12/23/14 19:13, 'Petros Moisiadis' via Django users wrote:
> Hello people :)
>
> I am struggling with optimizing an admin with inlines which causes too
> many database requests.
>
> My model structure is like this:
>
> class ExampleA(models.Model):
> ...
>
> class ExampleB(models.Model):
> aexample = models.ForeignKey('ExampleA', related_name='bexamples')
> cexample = models.ForeignKey('ExampleC')
> dexample = models.ForeignKey('ExampleD')
> eexample = models.ForeignKey('ExampleE')
> ...
>
> class ExampleC(models.Model):
> ...
>
> class ExampleD(models.Model):
> ...
>
> class ExampleE(models.Model):
> ...
>
> The admin classes:
>
> class ExampleBInline(admin.StackedInline):
> model = ExampleB
> extra = 0
>
> class ExampleAAdmin(admin.ModelAdmin):
> inlines = [ExampleBInline]
>
> admin.site.register(ExampleA, ExampleAAdmin)
>
> As I can see with django-debug-toolbar, when rendering the admin
> template for the inline formset with the forms for ExampleB objects, a
> new db request is sent to db server for each related field of each
> ExampleB object. Particularly, I am seeing a lot of queries like these:
>
> SELECT ••• FROM `examplec` WHERE `examplec`.`id` = 1 LIMIT 21
> SELECT ••• FROM `examplee` WHERE `examplee`.`id` = 2 LIMIT 21
> SELECT ••• FROM `examplec` WHERE `examplec`.`id` = 2 LIMIT 21
> SELECT ••• FROM `exampled` WHERE `exampled`.`id` = 2 LIMIT 21
> SELECT ••• FROM `examplee` WHERE `examplee`.`id` = 3 LIMIT 21
>
> The template context is this (I am using grappelli):
>
> 21{% if field.is_readonly %}
> 22{{
> field.contents|linebreaksbr }}
> 23{% else %}
> the marked line =>  24{{ field.field }}
> 25{% endif %}
> 26{% endif %}
> 27{% if line.fields|length_is:'1' %}{{
> line.errors }}{% endif %}
>
>
> I have tried the following optimizations:
>
> First try:
>
> class ExampleAAdmin(admin.ModelAdmin):
> inlines = [ExampleBInline]
>
> def get_queryset(self, request):
> qs = super(ExampleAAdmin, self).get_queryset(request)
> return qs.prefetch_related('bexamples',
> 'bexamples__cexample', 'bexamples__dexample', 'bexamples__example')
>
> Second try:
>
> class ExampleBInline(admin.StackedInline):
> model = ExampleB
> extra = 0
>   
> def get_queryset(self, request):
> qs = super(ExampleInline, self).get_queryset(request)
> return qs.select_related('cexample', 'dexample', 'eexample')
>
> Third try:
>
> class BaseExampleBFormSet(BaseInlineFormSet):
> def __init__(self, *args, **kwargs):
> super(BaseExampleBFormSet, self).__init__(*args, **kwargs)
> qs = self.queryset
> self.queryset = qs.select_related('cexample', 'dexample',
> 'eexample')
>
> ExampleBFormSet = inlineformset_factory(ExampleA, ExampleB,
> formset=BaseExampleBFormSet)
>
> class ExampleBInline(admin.StackedInline):
> model = ExampleB
> extra = 0
> formset = ExampleBFormSet
>
> Unfortunately, none of the above works.
>
> So, I would be really grateful if anyone could help on this by giving
> any hint or pointing out what could be possibly missing. Or, could I
> have hit a restriction of django admin's internals?
>
> Thank you in advance,
>
> Petros
>

Hello again,

I would like to clarify that the big number of db requests is not caused
by fetching all the objects for the related model to populace select
boxes for the related fields in the line, as it is often the case. To
avoid that, I have included the related fields in raw_id_fields. I have
also used grappelli's convenient autocomplete lookup feature, but that
does not add extra queries. So, the inline class actually looks like this:

class ExampleBInline(admin.StackedInline):
model = ExampleB
extra = 0
raw_id_fields = ['cexample', 'dexample', 'eexample']
autocomplete_lookup_fields = {
'fk': ['cexample', 'dexample', 'example'],
}

The only optimization that works is adding the related fields in
readonly_fields in combination with a queryset changing method with
select_related(), like this:

class ExampleBInline(admin.StackedInline):
model = ExampleB
extra = 0
raw_id_fields = ['cexample', 'dexample', 'eexample']
autocomplete_lookup_fields = {
'fk': ['cexample', 'dexample', 'eexample'],
}
readonly_fields = ['cexample', 'dexample', 'eexample']

def get_queryset(self, request):
qs = super(ExampleBInline, self).get_queryset(request)
return qs.select_related('cexample', 'dexample', 'eexample')


However, I am not satisfied with that 

Re: Optimizing admin change view with inlines

2014-12-24 Thread 'Petros Moisiadis' via Django users
On 12/24/14 13:30, 'Petros Moisiadis' via Django users wrote:
> On 12/23/14 19:13, 'Petros Moisiadis' via Django users wrote:
>> Hello people :)
>>
>> I am struggling with optimizing an admin with inlines which causes too
>> many database requests.
>>
>> My model structure is like this:
>>
>> class ExampleA(models.Model):
>> ...
>>
>> class ExampleB(models.Model):
>> aexample = models.ForeignKey('ExampleA', related_name='bexamples')
>> cexample = models.ForeignKey('ExampleC')
>> dexample = models.ForeignKey('ExampleD')
>> eexample = models.ForeignKey('ExampleE')
>> ...
>>
>> class ExampleC(models.Model):
>> ...
>>
>> class ExampleD(models.Model):
>> ...
>>
>> class ExampleE(models.Model):
>> ...
>>
>> The admin classes:
>>
>> class ExampleBInline(admin.StackedInline):
>> model = ExampleB
>> extra = 0
>>
>> class ExampleAAdmin(admin.ModelAdmin):
>> inlines = [ExampleBInline]
>>
>> admin.site.register(ExampleA, ExampleAAdmin)
>>
>> As I can see with django-debug-toolbar, when rendering the admin
>> template for the inline formset with the forms for ExampleB objects, a
>> new db request is sent to db server for each related field of each
>> ExampleB object. Particularly, I am seeing a lot of queries like these:
>>
>> SELECT ••• FROM `examplec` WHERE `examplec`.`id` = 1 LIMIT 21
>> SELECT ••• FROM `examplee` WHERE `examplee`.`id` = 2 LIMIT 21
>> SELECT ••• FROM `examplec` WHERE `examplec`.`id` = 2 LIMIT 21
>> SELECT ••• FROM `exampled` WHERE `exampled`.`id` = 2 LIMIT 21
>> SELECT ••• FROM `examplee` WHERE `examplee`.`id` = 3 LIMIT 21
>>
>> The template context is this (I am using grappelli):
>>
>> 21{% if field.is_readonly %}
>> 22{{
>> field.contents|linebreaksbr }}
>> 23{% else %}
>> the marked line =>  24{{ field.field }}
>> 25{% endif %}
>> 26{% endif %}
>> 27{% if line.fields|length_is:'1' %}{{
>> line.errors }}{% endif %}
>>
>>
>> I have tried the following optimizations:
>>
>> First try:
>>
>> class ExampleAAdmin(admin.ModelAdmin):
>> inlines = [ExampleBInline]
>>
>> def get_queryset(self, request):
>> qs = super(ExampleAAdmin, self).get_queryset(request)
>> return qs.prefetch_related('bexamples',
>> 'bexamples__cexample', 'bexamples__dexample', 'bexamples__example')
>>
>> Second try:
>>
>> class ExampleBInline(admin.StackedInline):
>> model = ExampleB
>> extra = 0
>>   
>> def get_queryset(self, request):
>> qs = super(ExampleInline, self).get_queryset(request)
>> return qs.select_related('cexample', 'dexample', 'eexample')
>>
>> Third try:
>>
>> class BaseExampleBFormSet(BaseInlineFormSet):
>> def __init__(self, *args, **kwargs):
>> super(BaseExampleBFormSet, self).__init__(*args, **kwargs)
>> qs = self.queryset
>> self.queryset = qs.select_related('cexample', 'dexample',
>> 'eexample')
>>
>> ExampleBFormSet = inlineformset_factory(ExampleA, ExampleB,
>> formset=BaseExampleBFormSet)
>>
>> class ExampleBInline(admin.StackedInline):
>> model = ExampleB
>> extra = 0
>> formset = ExampleBFormSet
>>
>> Unfortunately, none of the above works.
>>
>> So, I would be really grateful if anyone could help on this by giving
>> any hint or pointing out what could be possibly missing. Or, could I
>> have hit a restriction of django admin's internals?
>>
>> Thank you in advance,
>>
>> Petros
>>
> Hello again,
>
> I would like to clarify that the big number of db requests is not caused
> by fetching all the objects for the related model to populace select
> boxes for the related fields in the line, as it is often the case. To
> avoid that, I have included the related fields in raw_id_fields. I have
> also used grappelli's convenient autocomplete lookup feature, but that
> does not add extra queries. So, the inline class actually looks like this:
>
> class ExampleBInline(admin.StackedInline):
> model = ExampleB
> extra = 0
>   raw_id_fields = ['cexample', 'dexample', 'eexample']
>   autocomplete_lookup_fields = {
> 'fk': ['cexample', 'dexample', 'example'],
>   }
>
> The only optimization that works is adding the related fields in
> readonly_fields in combination with a queryset changing method with
> select_related(), like this:
>
> class ExampleBInline(admin.StackedInline):
> model = ExampleB
> extra = 0
>   raw_id_fields = ['cexample', 'dexample', 'eexample']
>   autocomplete_lookup_fields = {
> 'fk': ['cexample', 'dexample', 'eexample'],
>   }
>   readonly_fields = ['cexample', 'dexample', 'eexample']
>
>  

How to capture any incorrect typing of a url and pass it to a default page

2014-12-24 Thread pythonista
I want to code the urls.py such that if someone types in any garbage inn 
the browser the final line of the url router will grab the line and reroute 
it to some page(the home page) for example.

I added the following line of code to the bottom of the url  

url(r'^([a-zA-Z0-9\-\_]*)', views.my_default),   expecting that it would 
send it to my default view

So, it appears to be working and  not matching anything above that  url and 
falls all the way through, however I am getting any error

TypeError at /waitlist/dict

my_default() takes exactly 1 argument (2 given)



My view is very simple


def my_default_2(request):
context = RequestContext(request)
context_dict = {'boldmessage': "pass through from waitlist"}
return render_to_response('waitlist/home.html', context_dict, context)



So, it looks like it is getting to the view, but I am getting the error above.

Any suggestions as to why I am getting the argument error above.


Thanks



-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/67d57b9e-6d36-4c17-aee0-1fef5db37d79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: django problem with mysql and apache

2014-12-24 Thread Sandeep kaur
On Wed, Dec 24, 2014 at 6:54 AM,  wrote:
>
> 3 during the installation i have this probleme about mysql
>
>
>
> root@linux-pc:/home/siteweb# python3.4 manage.py migrate
> Traceback (most recent call last):
>   File
"/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py",
line 14, in 
> import MySQLdb as Database
> ImportError: No module named 'MySQLdb'

About this, try installing MySQLdb using this command:
apt-get install python-mysqldb

-- 
Sandeep Kaur
Blog: sandymadaan.wordpress.com
SCM: https://github.com/sandeepmadaan

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


Re: Two Django projects with common models and business logic

2014-12-24 Thread andy
Thank you. Since it's only me that'd be using the apps do can I bypass the 
'deploy using pip' thing and somehow directly use it in my other projects?

/andy

On Wednesday, December 24, 2014 12:41:25 AM UTC+5:30, Javier Guerra wrote:
>
> On Tue, Dec 23, 2014 at 1:22 PM, andy > 
> wrote: 
> > Now, both the projects have some models and some business logic common 
> > between them. I don't want to duplicate the code and data which shall be 
> > chaotic going forward. Also, I want the models and code (business logic) 
> to 
> > be in sync (when models/code is altered). 
>
>
> encapsulate those models and code in reusable apps.  and since Django 
> apps are just Python modules, you can package and deploy them using 
> Python tools like pip. 
>
> -- 
> Javier 
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4448040a-6db8-4558-ac30-e559d57fce6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to capture any incorrect typing of a url and pass it to a default page

2014-12-24 Thread Vijay Khemlani
Try adding the *args and **kwargs parameters

def my_default_2(request, *args, **kwargs):
...

On Wed, Dec 24, 2014 at 1:15 PM, pythonista 
wrote:

> I want to code the urls.py such that if someone types in any garbage inn
> the browser the final line of the url router will grab the line and reroute
> it to some page(the home page) for example.
>
> I added the following line of code to the bottom of the url
>
> url(r'^([a-zA-Z0-9\-\_]*)', views.my_default),   expecting that it would
> send it to my default view
>
> So, it appears to be working and  not matching anything above that  url
> and falls all the way through, however I am getting any error
>
> TypeError at /waitlist/dict
>
> my_default() takes exactly 1 argument (2 given)
>
>
>
> My view is very simple
>
>
> def my_default_2(request):
> context = RequestContext(request)
> context_dict = {'boldmessage': "pass through from waitlist"}
> return render_to_response('waitlist/home.html', context_dict, context)
>
>
>
> So, it looks like it is getting to the view, but I am getting the error above.
>
> Any suggestions as to why I am getting the argument error above.
>
>
> Thanks
>
>
>
>  --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/67d57b9e-6d36-4c17-aee0-1fef5db37d79%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei0_7oRjor9JJP9RhqEGE8rb%3DPaLNYTeTo3Pu41-Y0M-sA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Two Django projects with common models and business logic

2014-12-24 Thread Javier Guerra Giraldez
On Wed, Dec 24, 2014 at 11:18 AM, andy  wrote:
> Thank you. Since it's only me that'd be using the apps do can I bypass the
> 'deploy using pip' thing and somehow directly use it in my other projects?


you could just set links or add to your `sys.path` list, but that
makes it hard not to break one project when developing the other.

a little better is to use your version control system, so now you have
(at least) three projects: project A, project B and a common app, each
on a different repository.  each server would checkout not only the
project-specific code but also the common app

-- 
Javier

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


Re: Two Django projects with common models and business logic

2014-12-24 Thread Vijay Khemlani
You can version it under a different repository, clone it in each server,
and then add that path to the PYTHONPATH of each project, or just make a
symbolic link to the app.



On Wed, Dec 24, 2014 at 1:18 PM, andy  wrote:

> Thank you. Since it's only me that'd be using the apps do can I bypass the
> 'deploy using pip' thing and somehow directly use it in my other projects?
>
> /andy
>
> On Wednesday, December 24, 2014 12:41:25 AM UTC+5:30, Javier Guerra wrote:
>>
>> On Tue, Dec 23, 2014 at 1:22 PM, andy  wrote:
>> > Now, both the projects have some models and some business logic common
>> > between them. I don't want to duplicate the code and data which shall
>> be
>> > chaotic going forward. Also, I want the models and code (business
>> logic) to
>> > be in sync (when models/code is altered).
>>
>>
>> encapsulate those models and code in reusable apps.  and since Django
>> apps are just Python modules, you can package and deploy them using
>> Python tools like pip.
>>
>> --
>> Javier
>>
>  --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4448040a-6db8-4558-ac30-e559d57fce6c%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei1cCLez2vih7tQU4C_3cU-Rip2B0m5kCBb5Hv_G-MjHVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to configure django and MySql with python properly ??

2014-12-24 Thread Osman Goni Nahid
I'm beginner in django , can't configure properly django , mysqldb in 
python trying more time can't find where i make mistake . can anyone give 
me an video or link for me .
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/69280fd8-b177-4722-b396-4bb821285249%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.