Re: Optimizing admin change view with inlines
Hi Petros, I think the raw_id_fields still display the __str__/__unicode__ of the selected object so therefore need to get queried. I wonder if select_related would possibly help? Collin On Wednesday, December 24, 2014 5:53:02 AM UTC-6, Ernest0x wrote: > > 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 = ['cexam
Re: django problem with mysql and apache
Hi, 1. I don't do a lot of multilingual, but here's a start: https://docs.djangoproject.com/en/dev/topics/i18n/ If you have _content_ that's in multiple languages, you generally will want separate urls, like https://example.com/en/some/page/ 2. Not sure on the best forum software for django. 3. Like mentioned, run: apt-get install python-mysqldb or us the recommended mysqlclient apt-get install mysql-dev pip install mysqlclient 4. If you haven't already, check out the django's mod_wsgi docs: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/ Collin On Tuesday, December 23, 2014 7:24:16 PM UTC-6, th.gr...@free.fr wrote: > > Hello > i have just installed Django on Ubuntu 14.04 and i'd like to know > > 1 how to do a multilingual site? > 2 how to do a forum or use one existent > > 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' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "manage.py", line 10, in > execute_from_command_line(sys.argv) > File > "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", > line 385, in execute_from_command_line > utility.execute() > File > "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", > line 354, in execute > django.setup() > File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line > 21, in setup > apps.populate(settings.INSTALLED_APPS) > File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", > line 108, in populate > app_config.import_models(all_models) > File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", > line 202, in import_models > self.models_module = import_module(models_module_name) > File "/usr/lib/python3.4/importlib/__init__.py", line 109, in > import_module > return _bootstrap._gcd_import(name[level:], package, level) > File "", line 2231, in _gcd_import > File "", line 2214, in _find_and_load > File "", line 2203, in > _find_and_load_unlocked > File "", line 1200, in _load_unlocked > File "", line 1129, in _exec > File "", line 1448, in exec_module > File "", line 321, in > _call_with_frames_removed > File > "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py", > line 40, in > class Permission(models.Model): > File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", > line 124, in __new__ > new_class.add_to_class('_meta', Options(meta, **kwargs)) > File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", > line 299, in add_to_class > value.contribute_to_class(cls, name) > File > "/usr/local/lib/python3.4/dist-packages/django/db/models/options.py", line > 166, in contribute_to_class > self.db_table = truncate_name(self.db_table, > connection.ops.max_name_length()) > File "/usr/local/lib/python3.4/dist-packages/django/db/__init__.py", > line 40, in __getattr__ > return getattr(connections[DEFAULT_DB_ALIAS], item) > File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line > 242, in __getitem__ > backend = load_backend(db['ENGINE']) > File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line > 108, in load_backend > return import_module('%s.base' % backend_name) > File "/usr/lib/python3.4/importlib/__init__.py", line 109, in > import_module > return _bootstrap._gcd_import(name[level:], package, level) > File "", line 2231, in _gcd_import > File "", line 2214, in _find_and_load > File "", line 2203, in > _find_and_load_unlocked > File "", line 1200, in _load_unlocked > File "", line 1129, in _exec > File "", line 1448, in exec_module > File "", line 321, in > _call_with_frames_removed > File > "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", > line 17, in > raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) > django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: > No module named 'MySQLdb' > > > > > how can i repair this? > > > AND finally > 4 i'd like ti use my apache server instead of the server provided by > Django. I have made the modifications in the apacahe2.conf > but now?? > > how use apache instead of the internat server od Django? > > > MANY 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/djang
Re: How to configure django and MySql with python properly ??
Hi, What happens when you try? Is this Ubuntu 14.04? Collin On Wednesday, December 24, 2014 1:54:14 PM UTC-6, Osman Goni Nahid wrote: > > 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/54604e39-68b3-43f8-8c9a-0576378d3475%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Two Django projects with common models and business logic
Hi All, Another option which no one mentioned yet would be to keep all of the code for _both_ projects in the same repo and share common code that way. Collin On Tuesday, December 23, 2014 12:22:03 PM UTC-6, andy wrote: > > Hi, > > I have two Django Projects that have different use cases. There are > reached using different domains. They are hosted in two different servers. > Also each Django project has it's own database. > > 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). > > Can anyone guide me towards a pattern that can help me attain the required > architecture: 2 separate projects with common models and business logic. > > Thanks in advance. > > /andy > > P.S. This is my first post. So pardon me for breaking any forum etiquette. > -- 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/f9805037-b7a0-4f7a-8f3a-46e94aec8a44%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: initial dta for model in Django 1.7 or newer
Hi, You are asking about initial data? Use RunPython or RunSQL. https://docs.djangoproject.com/en/dev/ref/migration-operations/#special-operations Collin On Thursday, December 25, 2014 9:06:35 AM UTC-6, Robert Jonathan Šimon wrote: > > Hi, > When i googled for this problem a found out, that with migrations in > Django 1.7, there is new way how it should be done, but i didnt understand > it how. Can anyone explain me the new way how to do it? > Thanks a lot. > -- 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/4812b852-2083-49e4-922f-2e1d01ca8c8e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
uni-code error in python : 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
Hi I use django and in my view i need to send a request as XML with some uni-code character that received from html page with post method. I tried these (Note that i save that input in fname variable) : xml = r"""my XML code with uni-code {0} """.format(fname) And fname = u"%s".encode('utf8') % (fname) xml = r"""my XML code with uni-code {0} """.format(fname) And fname = fname.encode('ascii', 'ignore').decode('ascii') xml = r"""my XML code with uni-code {0} """.format(fname) And every time i got this error: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) Please help me. -- 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/ae41cdad-dc75-49be-8d93-ef308e2fe0f2%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: uni-code error in python : 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
I Solved that with this code: fname = fname.encode('ascii', 'xmlcharrefreplace') xml = r"""my XML code with unicode {0} """.format(fname) On Monday, December 29, 2014 11:03:49 AM UTC+3:30, Hossein Rashnoo wrote: > > Hi > I use django and in my view i need to send a request as XML with some > uni-code character that received from html page with post method. I tried > these (Note that i save that input in fname variable) : > > xml = r"""my XML code with uni-code {0} """.format(fname) > > And > > fname = u"%s".encode('utf8') % (fname) > xml = r"""my XML code with uni-code {0} """.format(fname) > > And > > fname = fname.encode('ascii', 'ignore').decode('ascii') > xml = r"""my XML code with uni-code {0} """.format(fname) > > And every time i got this error: > > 'ascii' codec can't encode characters in position 0-3: ordinal not in > range(128) > > Please help me. > -- 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/bc6c1f66-6c1e-49b8-baba-7431cb2e2d4f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.