Concurrent Web Access and Edits
New Django user here with a question about the client side: Is there built-in support for dealing with concurrent edits? I'm not referring to the database issues (locking, etc) but is there Django support for handling a request to access or edit data for which another user is editing? I'm thinking about simple timestamps, detection of changed data, and showing the user options to abort, overwrite, or re-edit. Does one have to implement this for saving data? Anything I should read that covers this or related topic? I couldn't find anything on the Django site, nor in this forum. Thanks for any pointers! -Doug --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Bulk data insert
On Aug 20, 2:50 pm, Alex Gaynor wrote: > On Thu, Aug 20, 2009 at 1:46 PM, Abiel wrote: > > > Is there an efficient way to use Django models to load a large number > > of records into a database without falling back on raw SQL? Creating a > > large number of model objects and then saving each one individually is > > very slow (I imagine Django is running INSERT and COMMIT each time?). > > > Thanks very much. > > Django doesn't currently support any form of bulk insert. Using raw > SQL is your best option at this point. > I also am trying to do a bulk load, but has to be written in Python. It is a two pass loading procedure, where the first pass could be done with the indexes turned off, and the second pass with them on. Is there a way to disable indexes momentarily and then turn them back on? Or can I manage the transactions to do saves in bulk in the first pass? Thanks for any pointers! -Doug > Alex > > -- > "I disapprove of what you say, but I will defend to the death your > right to say it." -- Voltaire > "The people's good is the highest law." -- Cicero > "Code can always be simpler than you think, but never as simple as you > want" -- Me --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Bulk data insert
On Thu, Aug 20, 2009 at 10:40 PM, Benjamin Sergeant wrote: > > (the answer was already sent, raw SQL, anyway) > > (with postgresql) I would try to load just a small bit of datas with > the slow create django object / save it process, then do a pg sql > dump, and look at the sql that pg dump generated. Then code a python > script that generate that. > > And then do a pg load which is super fast. > Let me be more specific about what I am looking for: I am writing an application that could have any django-supported backend. I have a two pass importer that fills in tables (the program does a lot of processing and data manipulation in these import passes). The application is written in django. I am looking for a way to speed up the first pass of the process that doesn't need indexes (of course, I'd speed up all of it if I could). I do not want to rewrite what I have as raw SQL, I am just looking for anything that I can do, such as turn off indexing, write to the db in batches, or similar django-level optimizations. I don't think what you mentioned can help in this situation, right? -Doug > > - Benjamin > > > > > On Thu, Aug 20, 2009 at 7:08 PM, Doug Blank wrote: > > > > On Aug 20, 2:50 pm, Alex Gaynor wrote: > >> On Thu, Aug 20, 2009 at 1:46 PM, Abiel wrote: > >> > >> > Is there an efficient way to use Django models to load a large number > >> > of records into a database without falling back on raw SQL? Creating a > >> > large number of model objects and then saving each one individually is > >> > very slow (I imagine Django is running INSERT and COMMIT each time?). > >> > >> > Thanks very much. > >> > >> Django doesn't currently support any form of bulk insert. Using raw > >> SQL is your best option at this point. > >> > > > > I also am trying to do a bulk load, but has to be written in Python. > > It is a two pass loading procedure, where the first pass could be done > > with the indexes turned off, and the second pass with them on. > > > > Is there a way to disable indexes momentarily and then turn them back > > on? Or can I manage the transactions to do saves in bulk in the first > > pass? > > > > Thanks for any pointers! > > > > -Doug > > > >> Alex > >> > >> -- > >> "I disapprove of what you say, but I will defend to the death your > >> right to say it." -- Voltaire > >> "The people's good is the highest law." -- Cicero > >> "Code can always be simpler than you think, but never as simple as you > >> want" -- Me > > > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: db api
On Fri, Aug 21, 2009 at 4:22 AM, luca72 wrote: > > I have solved using : > Model.object.filter(id=2)values('columnname') > Note that it should have a dot: Model.object.filter(id=2).values('columnname') and that returns the item you want in a list. The proper way to do that is: Model.object.get(id=2).columnname where get returns an object, and all of the columnnames are just attributes of it. -Doug > > Thanks > > Luca > > On 21 Ago, 10:14, luca72 wrote: > > Thanks for your reply but i get this error: > > Model object has no attribute 'values' > > > > Thanks > > > > On 21 Ago, 09:42, Kenneth Gonsalves wrote: > > > > > On Friday 21 Aug 2009 1:00:32 pm luca72 wrote: > > > > > > how i can write this with the djago db api: > > > > SELECT "column name" from "table_name" where "id"=2 > > > > > Model.objects.get(id=2).values('columnname',) > > > -- > > > regards > > > kghttp://lawgon.livejournal.com > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Dates with varying specificity?
On Sat, Aug 22, 2009 at 12:24 PM, ringemup wrote: > > Has anyone here ever written an application that needed to be able to > take a date field where the month or day was sometimes but not always > known? How would you handle this? Using separate day/month/year > fields instead of a DateField seems to sacrifice a lot of convenience > and usability. > We've just begun to develop a web front-end to an open source genealogy program (http://gramps-project.org) and are looking into this issue. We have a sophisticated date object that includes different calendars (Julian, Gregorian, etc) and date specifiers ("about 1863", "between Jan 1, 1734 and Feb 1735") and more. We'll probably develop our own Model so that we can treat these with some of the same convenience as DateField, but handle the complexity from behind. We don't have any of the date model coded yet, so this is just an FYI. -Doug > > Thanks! > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: newbye question
On Mon, Sep 14, 2009 at 7:17 AM, Nicola Manica wrote: > Hi, I'm a student that try to learn geodjango. > I have the following code in one view: > ... > for t_path in path.the_geom: > print t_path.json > print t_path.wkt > print t_path.kml > ... > > path.the_geom is a multilinestring. > The output that I obtain is > ... > { "type": "LineString", "coordinates": [ [ 11.128620, 46.056345 ], [ > 11.128456, 46.056653 ], [ 11.128398, 46.056771 ] ] } > LINESTRING (11.12862023 46.056345100015, 11.12845566 > 46.05665337, 11.12839815 46.05677077) > 11.1286202,46.0563451,0 11.1284556,46.0566533,0 > 11.1283982,46.056771,0 > > > json is correct and also wkt. > But kml? Is it correct? Why some points have coordinates equal to zero? > Thank for your help > Looks like x,y,z coordinates if I were guessing. If you had elevation data, maybe it wouldn't be zeros. -dsb > Nicola > > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
settings.configure vs DJANGO_SETTINGS_MODULE
I'm trying to use django without resorting to setting the environment variable, by doing this: from django.conf import settings import mysettings settings.configure(mysettings, DEBUG=True) The mysettings.py originally just contained those in the default settings.py file. However, I keep getting errors claiming that my module is missing settings, such as this error: ... File "/usr/lib/python2.6/site-packages/django/utils/translation/trans_real.py", line 194, in translation default_translation = _fetch(settings.LANGUAGE_CODE) File "/usr/lib/python2.6/site-packages/django/utils/translation/trans_real.py", line 172, in _fetch for localepath in settings.LOCALE_PATHS: File "/usr/lib/python2.6/site-packages/django/utils/functional.py", line 273, in __getattr__ return getattr(self._wrapped, name) File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 130, in __getattr__ return getattr(self.default_settings, name) AttributeError: 'module' object has no attribute 'LOCALE_PATHS' I've added a bunch of settings with their defaults after I get such an error, such as these: DATABASE_OPTIONS = '' URL_VALIDATOR_USER_AGENT = '' DEFAULT_INDEX_TABLESPACE = '' DEFAULT_TABLESPACE = '' CACHE_BACKEND = 'locmem://' TRANSACTIONS_MANAGED = False and it will run further into some new code, until django looks up another setting, and it isn't there. Everything works fine if I set the DJANGO_SETTINGS_MODULE env variable. I've read: http://docs.djangoproject.com/en/dev/ref/settings/ and the older version for 1.0, but they both suggest that what I'm doing is correct. I'm using sqlite and Django-1.1-4.fc11.noarch under Fedora 11. Is this a bug, or am I doing something wrong? Thanks for any insight! -Doug --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: settings.configure vs DJANGO_SETTINGS_MODULE
On Tue, Oct 13, 2009 at 10:06 PM, Doug Blank wrote: > I'm trying to use django without resorting to setting the environment > variable, by doing this: > > from django.conf import settings > import mysettings > settings.configure(mysettings, DEBUG=True) > > The mysettings.py originally just contained those in the default > settings.py file. > > However, I keep getting errors claiming that my module is missing > settings, such as this error: > > ... > File > "/usr/lib/python2.6/site-packages/django/utils/translation/trans_real.py", > line 194, in translation > default_translation = _fetch(settings.LANGUAGE_CODE) > File > "/usr/lib/python2.6/site-packages/django/utils/translation/trans_real.py", > line 172, in _fetch > for localepath in settings.LOCALE_PATHS: > File "/usr/lib/python2.6/site-packages/django/utils/functional.py", > line 273, in __getattr__ > return getattr(self._wrapped, name) > File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", > line 130, in __getattr__ > return getattr(self.default_settings, name) > AttributeError: 'module' object has no attribute 'LOCALE_PATHS' > > I've added a bunch of settings with their defaults after I get such an > error, such as these: > > DATABASE_OPTIONS = '' > URL_VALIDATOR_USER_AGENT = '' > DEFAULT_INDEX_TABLESPACE = '' > DEFAULT_TABLESPACE = '' > CACHE_BACKEND = 'locmem://' > TRANSACTIONS_MANAGED = False > > and it will run further into some new code, until django looks up > another setting, and it isn't there. > > Everything works fine if I set the DJANGO_SETTINGS_MODULE env variable. > > I've read: > > http://docs.djangoproject.com/en/dev/ref/settings/ > > and the older version for 1.0, but they both suggest that what I'm > doing is correct. > > I'm using sqlite and Django-1.1-4.fc11.noarch under Fedora 11. > > Is this a bug, or am I doing something wrong? > > Thanks for any insight! > > -Doug > I was able to make it through all of the tests with my sqlite backend by adding all of these default values: DATABASE_OPTIONS = '' URL_VALIDATOR_USER_AGENT = '' DEFAULT_INDEX_TABLESPACE = '' DEFAULT_TABLESPACE = '' CACHE_BACKEND = 'locmem://' TRANSACTIONS_MANAGED = False LOCALE_PATHS = tuple() But I'm wondering if I change the backend if there will be other missing values. Do people not use settings.configure()? I would think that most people would not want to have to rely on an environment variable. -Doug --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Using variables as keywords in QuerySet
On Wed, Oct 21, 2009 at 5:12 AM, Arthur Metasov wrote: > > 2009/10/21 valler <180...@gmail.com>: >> >> Hello. I want to know, if something like that possible: >> MyModel.objects.filter(name__contains='foo',status__exact='bar') => >> It's OK. >> >> But I want to use dynamic keyword generation, like that: >> a='name' >> b='status' >> MyModel.objects.filter(a__contains='foo',b__exact='bar') >> >> Is it possible somehow? > > a='name' > b='status' > keyword_arguments = {} > keyword_arguments[a+'__contains']='foo'; > keyword_arguments[b+'__exact']='bar'; > MyModel.objects.filter(**keyword_arguments) > Or, more concisely as: MyModel.objects.filter(**{a+'__contains':'foo', b+'__exact':'bar';}) -Doug > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Automatic swapping of field data?
Django users, I have data that needs to be handled in two different manners, depending on if the user has certain permissions or not. For example, if a record is marked "private" and a user is not permitted, I want to substitute the word "PROTECTED" for a particular field's value. Now, of course I realize that each and every place I refer to table.fieldname I could wrap a protection around that, either in my Python code, or in my templates. What I'm really looking for is something closer to the model code so that I can be assured that no private data accidentally slips out. Does Django have any built in support that does this, or could be adapted to do this? Any ideas appreciated! -Doug -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: Automatic swapping of field data?
On Thu, Nov 19, 2009 at 4:36 PM, Preston Holmes wrote: > There are some details left out of how you want this to look. > > The lower the level you try to make an object self protecting, the > trickier it's going to be. > > The sweet spot I think here is a custom manager that adds a protect > filter, and perhaps a subclass of ModelForm if you need this in > forms. The custom manager method would take a user obj as a > parameter, then loop through the objects in the queryset and modify > protected fields as needed. > > http://www.djangoproject.com/documentation/models/custom%5Fmanagers/ > http://www.djangosnippets.org/snippets/562/ Thanks for the links! These got me thinking along a line of thought, and I came up with something like: class MyModel(models.Model): ... def __getattribute__(self, item): if item == "fieldname": return "" else: return super(MyModel, self).__getattribute__(item) In the getattribute method, I can check some conditions (ie, to see if the user is logged in), and provide my own data if not. This way, I can write my django code and never have to worry that the value of fieldname will ever accidentally not be protected as this is the only way to get it. See any problems with this (other than taking a small hit on every field access)? -Doug > On Nov 18, 4:17 pm, Doug Blank wrote: >> Django users, >> >> I have data that needs to be handled in two different manners, >> depending on if the user has certain permissions or not. For example, >> if a record is marked "private" and a user is not permitted, I want to >> substitute the word "PROTECTED" for a particular field's value. >> >> Now, of course I realize that each and every place I refer to >> table.fieldname I could wrap a protection around that, either in my >> Python code, or in my templates. >> >> What I'm really looking for is something closer to the model code so >> that I can be assured that no private data accidentally slips out. >> Does Django have any built in support that does this, or could be >> adapted to do this? >> >> Any ideas appreciated! >> >> -Doug > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
MemoryError when adding a lot of data
I'm filling my Django tables with data through a regular Python program (not through the browser). After it runs for a few hours, I get: 10557896: ERROR: gramps.py: line 121: Unhandled exception Traceback (most recent call last): ... event = models.Event.objects.get(handle=ref) File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get return self.get_query_set().get(*args, **kwargs) File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get num = len(clone) File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__ self._result_cache = list(self.iterator()) File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator for row in self.query.results_iter(): File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter for rows in self.execute_sql(MULTI): File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql cursor.execute(sql, params) File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 22, in execute sql = self.db.ops.last_executed_query(self.cursor, sql, params) File "/usr/lib/python2.6/site-packages/django/db/backends/__init__.py", line 217, in last_executed_query return smart_unicode(sql) % u_params MemoryError The line that looks suspicious to me is the "self._result_cache = list(self.iterator())". Does it look like that might be the problem? I'm not manually doing anything other than adding data, changing data, and calling .save() (eg, I'm not doing anything with transactions in my code). I looked at the chapter on cache from the book: http://www.djangobook.com/en/beta/chapter14/ but I'm not sure how this is related. Although, if there was a Python command to turn off caching while importing, that might be something to try. Any ideas appreciated, -Doug -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: MemoryError when adding a lot of data
On Fri, Nov 20, 2009 at 6:32 AM, Doug Blank wrote: > I'm filling my Django tables with data through a regular Python > program (not through the browser). After it runs for a few hours, I > get: > > 10557896: ERROR: gramps.py: line 121: Unhandled exception > Traceback (most recent call last): > ... > event = models.Event.objects.get(handle=ref) > File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", > line 120, in get > return self.get_query_set().get(*args, **kwargs) > File "/usr/lib/python2.6/site-packages/django/db/models/query.py", > line 300, in get > num = len(clone) > File "/usr/lib/python2.6/site-packages/django/db/models/query.py", > line 81, in __len__ > self._result_cache = list(self.iterator()) > File "/usr/lib/python2.6/site-packages/django/db/models/query.py", > line 238, in iterator > for row in self.query.results_iter(): > File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", > line 287, in results_iter > for rows in self.execute_sql(MULTI): > File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", > line 2369, in execute_sql > cursor.execute(sql, params) > File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", > line 22, in execute > sql = self.db.ops.last_executed_query(self.cursor, sql, params) > File "/usr/lib/python2.6/site-packages/django/db/backends/__init__.py", > line 217, in last_executed_query > return smart_unicode(sql) % u_params > MemoryError > > The line that looks suspicious to me is the "self._result_cache = > list(self.iterator())". Does it look like that might be the problem? > I'm not manually doing anything other than adding data, changing data, > and calling .save() (eg, I'm not doing anything with transactions in > my code). > > I looked at the chapter on cache from the book: > > http://www.djangobook.com/en/beta/chapter14/ > > but I'm not sure how this is related. Although, if there was a Python > command to turn off caching while importing, that might be something > to try. > > Any ideas appreciated, > > -Doug > Some additional data: I'm using Django 1.1 on Fedora11 with sqlite backend. I get the same kind of spiking of memory usage if I just enter: >>> Person.objects.all().delete() Memory usage continues to grow, and it doesn't seem to be able to be garbage collected. What could cause this? Anything I can set or issue to make Django clean up/use less memory? I am running out of memory, just deleting the data! Something seems to be very wrong... -Doug -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: MemoryError when adding a lot of data
On Fri, Nov 20, 2009 at 10:22 PM, Karen Tracey wrote: > On Fri, Nov 20, 2009 at 6:19 PM, Doug Blank wrote: >> >> Some additional data: >> >> I'm using Django 1.1 on Fedora11 with sqlite backend. I get the same >> kind of spiking of memory usage if I just enter: >> >> >>> Person.objects.all().delete() >> >> Memory usage continues to grow, and it doesn't seem to be able to be >> garbage collected. What could cause this? Anything I can set or issue >> to make Django clean up/use less memory? I am running out of memory, >> just deleting the data! Something seems to be very wrong... >> > > Are you running this script with settings that have DEBUG set to True? I did have DEBUG = False in settings.py, but I also confirmed that it was the connections that were growing. On further inspection, I found I had this tucked away in my non-browser, reloadable Python code: from django.conf import settings import web.settings as default_settings try: settings.configure(default_settings, DEBUG=True) except RuntimeError: # already configured; ignore pass which was causing part of the problem. It does seem that: Table.objects.all().delete() is "leaking" memory (eg, continues to use memory) and is very slow. I'm trying to find a better (faster, less memory) method similar to the manage.py command sql_flush... Thank you very much for the pointer! Part of the problem solved... -Doug > See: > http://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory > > Karen ooglegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: MemoryError when adding a lot of data
On Sat, Nov 21, 2009 at 7:44 AM, Doug Blank wrote: [snip] > It does seem that: > > Table.objects.all().delete() > > is "leaking" memory (eg, continues to use memory) and is very slow. > I'm trying to find a better (faster, less memory) method similar to > the manage.py command sql_flush... Is this a "better" way to flush a table, programmatically? from django.db import connection, transaction models = [Person, User, ...] cursor = connection.cursor() flush_tables = [] for model in models: flush_tables.append(model._meta.db_table) statements = connection.ops.sql_flush(no_style(), flush_tables, connection.introspection.sequence_list()) for statement in statements: cursor.execute(statement) transaction.commit_unless_managed() It would be nice if flush() were a method on Model, yes? -Doug > Thank you very much for the pointer! Part of the problem solved... > > -Doug > >> See: >> http://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory >> >> Karen -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: form fields slicing
On Mon, Nov 23, 2009 at 7:16 AM, gentlestone wrote: > why this piece of code doesn't work? > > {% for field in form|slice:":2" %} > > the result is iterating over all fields, not just the first two fields Perhaps break this up into two parts? {% with form|slice:":2" as formslice %} (% for field in formslice %} -Doug > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: Model inheritance - filtering base model only
On Mon, Nov 23, 2009 at 9:09 AM, lfrodrigues wrote: > I guess this solution works but for +50 the performance should be > terrible... > > Shouldn't django have some option for this? Could you set a field's value which is true for one, and false for the other? Otherwise, could you just have two tables? I wonder if ContentType could help? obj_type = ContentType.objects.get_for_model(player) source_refs = PlayerM.filter(object_type=obj_type) (object_type gets created with generic relations) -Doug > On 23 Nov, 04:53, Preston Holmes wrote: >> Perhaps there is a more efficient way, but in my quick test, one can't >> filter() a queryset based on __class__ of the model, but seems one can >> manually filter it afterwords: >> >> qs = Player.objects.all() >> for i,obj in enumerate(qs): >> if obj.__class__ != Player: >> del(qs[i]) >> >> On Nov 22, 4:32 pm, lfrodrigues wrote: >> >> > Hello, >> >> > I have these models: >> >> > class Player(models.Model): >> > . >> >> > class PlayerM(Player): >> > ... >> >> > If I do PlayerM.objects.all() e get all PlayerM objects and for >> > Player.objects.all() I get all Player and PlayerM as expected. >> >> > How can get only the objects of type Player (only retrieve the objects >> > that were created with Player() constructor)? >> >> > Regards, >> >> > Luis > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: delete every record in a table?
On Fri, Dec 4, 2009 at 3:44 PM, Daniel Roseman wrote: > On Dec 4, 8:37 pm, Phlip wrote: >> Django users: >> >> I only ask the question to help improve my esthetics, but others might >> need a performance boost here. >> >> How to delete every record in a table? >> >> A related question - how to delete every record matching a filter, out >> of one internal SQL command? >> >> The brute-force way: >> >> for p in Plutocrat.objects.all(): p.delete() >> >> Is there a way to do that in one method call? and could such a method >> call only send one SQL command? >> > > Yes: > Plutocrat.objects.all().delete() I found that quite slow. If you want to stay DB agnostic, I came up with this: from django.db import connection, transaction models = [Person, User, ...] cursor = connection.cursor() flush_tables = [] for model in models: flush_tables.append(model._meta.db_table) statements = connection.ops.sql_flush(no_style(), flush_tables, connection.introspection.sequence_list()) for statement in statements: cursor.execute(statement) transaction.commit_unless_managed() I don't know of any bad side effects... -Doug > -- > DR. > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: import twice when run test
On Thu, Nov 26, 2009 at 6:01 AM, Tom Evans wrote: > On Thu, Nov 26, 2009 at 2:35 AM, fallhunter wrote: >> >> i have this code in my tests.py: >> >> from models import * >> >> and in the models.py I have a signal handler and register it with >> >> post_save.connect( post_save_note, sender=Note ) >> >> and when i run test with ./manage.py test main >> >> I found the signal handler was registered twice and executed twice, >> and I found it's because the models was imported twice. >> >> can't i put "from models import *" in the test code? what should i do? >> > > This happens if you import the same file using two different names. Eg, here > you are importing it as 'from models import *'. In some other part of your > code, you probably have 'from foo.models import *'. Python treats them as > two different files, and hence the signal is attached twice. BTW, this is not true for regular Python imports: $ cat > mytest.py print "hello" $ python >>> import mytest hello >>> from mytest import * >>> >>> import mytest as yourtest >>> It may be loaded twice (or more) by different threads or by another import mechanism (which seems to be the case here): http://code.djangoproject.com/wiki/Signals See section on "Help, post_save seems to be emitted twice for each save!" -Doug > Remove/rework the spurious import, and everything will work. > > Cheers > > Tom > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Using Forms to display and edit?
Django users, I'm wrestling with how to best create HTML pages that can either be used for displaying or editing data. That is, I'd like the field's values to appear as text in display mode, but in their widgets when in edit/add mode. It appears that Django wasn't designed to do this: the fields always appear in their widgets (eg, text input, text area, etc). Is there a common technique for handling this, short of using forms for one, and not the other? I was thinking of a custom templatetag filter that could be used for every form field, like: {{ form.field_name|render_field:mode }} where render_field would either return the field's HTML widget, or just the value as text, based on the mode. Have I missed something, or is this a viable solution? -Doug -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Using Forms to display and edit?
On Tue, Dec 15, 2009 at 5:45 PM, Margie Roginski wrote: > When I want to just display data in a form (as opposed to letting user > edit it), I create a specialized widget. For example, I have a model > that has multiple datetime fields, and I don't want the user to be > able to edit them, but I do want to display their values. Here's a > widget I use where you pass it some when you intiailize it (the task > it's operating on and the field name to display). Its render() method > just renders the specified field: > > class TaskForm_DisplayDateTimeWidget(widgets.Widget): > > def __init__(self, task=None, fieldNameToDisplay=None, attrs = > {}): > self.task = task > self.fieldNameToDisplay=fieldNameToDisplay > super(TaskForm_DisplayDateTimeWidget, self).__init__(attrs) > > def render(self, name, value, attrs=None): > rendered = "" > if self.task and self.fieldNameToDisplay: > dateVal = getattr(self.task, self.fieldNameToDisplay) > if dateVal: > rendered = mark_safe("%s" % date(dateVal, "%s %s" % > (settings.DATE_FORMAT, settings.TIME_FORMAT))) > > return rendered > > In my form, which is a model form, I declare the field like this in > "class global" area (l the area that is in the class but not in any > method: > > displayModified = DisplayField(label="modified", widget = > DisplayDateTimeWidget, required=False) > > Then in __init__() I do this: > > instance = kwargs.get("instance") > if instance: > self.fields["displayModified"].widget = > DisplayDateTimeWidget(task=instance, fieldName="displayModified") > > My form is a modelForm, so I do not put this field in Meta.fields, as > that would cause django to do some extra stuff like atempt to clean > the field, which doesn't make any sense since there is no input. So > basically I'm using all the form and widget infrastructure that django > supplies, but I'm just not sending any inputs associated with these > fields. I've seen a lot of people complain about django's lack of > "read only" fields, but it seems to me that being able to write your > own widgets and fields gives you total flexibility and that there's no > need for an actual "read only" field. My experience is that as soon > as I got past the basics in my project and wanted to do more complex > html/css, I never wanted to use the default rendering for any kind of > field, and read only fields are just one case of this. > > In your case you say you sometimes want to create the fields as > editable and soemtimes not. In your form you could look at a GET > variable that identifies whether you want fields to be editable or not > and then either create your field with a different widget based on > that GET variable, or even modify what your widget does based on that > GET variable (ie, pass the value of the GET variable in as a parameter > to the widgets __init__() method so it can do different stuff based on > it at render time. > > Anyway, hope this helps and am curious if others use this same > mechanism or if there is some alternate preferred approach. > > Margie Thanks Margie for this example. I may come to something similar to this, but for the time being I think I'll just put a filter in the template. That way, I'll have all of the elements there (errors, display information) for all of the modes (display, edit, and add). I'll see how far I can go with this method before having to resort to a more substantial widget-swap. It does seem that this is an area of Django that could use some thinking... -Doug > > > On Dec 15, 4:01 am, Doug Blank wrote: >> Django users, >> >> I'm wrestling with how to best create HTML pages that can either be >> used for displaying or editing data. That is, I'd like the field's >> values to appear as text in display mode, but in their widgets when in >> edit/add mode. It appears that Django wasn't designed to do this: the >> fields always appear in their widgets (eg, text input, text area, >> etc). >> >> Is there a common technique for handling this, short of using forms >> for one, and not the other? >> >> I was thinking of a custom templatetag filter that could be used for >> every form field, like: >> >> {{ form.field_name|render_field:mode }} >> >> where render_field would either return the field's HTML widget, or >> just the value as text, based on the mode. >> >> Have I missed something, or is this a viable solution? >> >>
Re: Custom Django-Admin commands
On Mon, Dec 21, 2009 at 8:10 PM, Justin Steward wrote: > Hi all, > > I've written a custom command to use with manage.py, and from the root > of the project directory, it works great. > > But the problem is, this command is almost never going to be called > from within the project directory. > > (hoping the spacing doesn't get too mangled when I send this) > /home/ > user/ <- ""/home/user/proj/manage.py custom" command doesn't exist. > proj/ <- "/home/user/proj/manage.py custom" works > manage.py > app/ > management/ > commands/ > custom.py > > Is there something I'm missing? You probably just need to set your PYTHONPATH: cd /home/user PYTHONPATH=proj python proj/manage.py custom -Doug > > ~Justin > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: super() argument 1 must be type, not None
On Wed, Dec 23, 2009 at 11:15 AM, TiNo wrote: > On Wed, Dec 23, 2009 at 16:53, Bill Freeman wrote: >> >> Are you sure that Lid is defined in this context? Try putting: >> >> import pdb;pdb.set_trace() >> >> before the super and check. > > As I described, I checked that. The strange thing is, that it is defined as > ``None``. As are all other imports, as ``datetime``, ``from django.db import > models``. All these imported libraries and classes, as well as classes > defined in that file are None for some reason. Quite weird... Perhaps this is a cyclic Python import issue. I think Python will go through a file twice, the first time getting names into the scope, and then a second time to fill in the details. Perhaps you are still in the middle of importing when you are attempting to use the partially loaded items. Another way of looking at this is that an import statement is atomic... no code will (should) run until all imports have completed, then the code runs. I would try importing just some parts, or maybe changing the import order. Just some ideas. Good luck! -Doug > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: get_absolute_url not recognized
On Tue, Dec 22, 2009 at 2:21 PM, neridaj wrote: > I'm trying to add a get_absolute_url method to a Tweet model from > django-syncr and do not understand why the method is not recognized. > > class Tweet(models.Model): > pub_time = models.DateTimeField() > twitter_id = models.PositiveIntegerField() > text = models.TextField() > user = models.ForeignKey('TwitterUser') > > def __unicode__(self): > return u'%s %s' % (self.user.screen_name, self.pub_time) > > def url(self): > return u'http://twitter.com/%s/statuses/%s' % > (self.user.screen_name, self.twitter_id) > > �...@models.permalink > def get_absolute_url(self): > return ('blog_tweet_detail', (), { 'year': > self.pub_time.strftime > ("%Y"), > 'month': > self.pub_time.strftime("%b").lower(), > 'day': > self.pub_time.strftime("%d"), > 'slug': > self.tweet.twitter_id }) > > from syncr.twitter.models import Tweet t = Tweet.objects.get(pk=1) t > t.url > > t.get_absolute_url > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'Tweet' object has no attribute 'get_absolute_url' What does: > Tweet.get_absolute_url give you? Perhaps this is a class method only? -Doug > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Create files and directories from Django 1.2.1
On Mon, Aug 16, 2010 at 7:23 AM, Mark Mooij wrote: > Allright I tried a couple of things with your suggested appraoch: > > - I printed all the strings used to define paths and files, this all > seems correct. > > - The os.mkdir() command is passed without raising an error, but > without creating the directory either, this results in an "IOError: No > such file or directory" because of course a file can't be created in a > non-existing directory. > > - I tried creating files in different already existing directories and > this works as expected. > > - I tried to use os.makedirs() instead of os.mkdir(), this raises an > interesting error: "mkdir() takes exactly 1 argument (2 given)", I > tried calling the script outside of Django and this should work the > way I'm using it: > > kmlpath = "%s/%s" % (basepath, game_id) > if not os.path.isdir(kmlpath): > os.makedirs(kmlpath) > > - I tried creating a file without any directory structure in the path, > the file is created in the root of the website, if I try creating a > directory with os.mkdir() it should take the root as well I presume, > but no directory is created. > > It seems that Django 1.2.1 has problems calling the os.mkdir() and > makedirs() commands ? I doubt it. You shouldn't really do this: kmlpath = "%s/%s" % (basepath, game_id) but do this: kmlpath = os.path.join(basepath, game_id) as different OS's have different path formats. Or perhaps it is the kml string that you are creating that has other issues (unicode?) I would print out the mkdirs string to see what you are creating. HTH, -Doug > Mark > > On 16 aug, 08:32, Mike Dewhirst wrote: >> On 16/08/2010 12:57pm, Mike Dewhirst wrote: >> >> >> >> >> >> > On 16/08/2010 1:06am, Mark Mooij wrote: >> >> Hi Mike, >> >> >> Thanks for your reply. I haven't changed anything else, I am >> >> processing the MS updates, but I don't think this is the problem, as >> >> this all worked fine before the migration to 1.2.1, also if I call the >> >> script directly (outside Django) it works fine. >> >> My view looks like this: >> >> .. >> >> import createKML >> >> .. >> >> def my_view(request, arg): >> >> ... >> >> createKML.createKML(arg) >> >> > I don't really know what is happening here that might have changed in >> > 1.2.1 maybe someone else can suggest something. >> >> > In the meantime here is suggested approach to discovering the exact >> > problem ... >> >> > import os >> > ... >> > def createKML(id, basepath="D:/path/to/mysite/templates/maps"): >> > ... >> > try: >> > if id: >> > kmlpath = "%s/%s" % (basepath, str(id)) >> >> actually .. kmlpath = "%s/%s" % (basepath, id) >> >> > if not os.path.isdir(kmlpath): >> > os.makedirs(kmlpath) >> > writefile = "%s/%s" % (kmlpath, str(id)) >> >> actually .. writefile = "%s/%s" % (kmlpath, id) >> >> >> >> > f = open(writefile, 'wb') >> > else: >> > raise >> > except Exception as e: >> > print("id = %s\nbasepath = %s\n%s" % (id, basepath, e) >> > ... >> >> > mike >> >> >> ... >> >> >> In the createKML.py the problem arises in: >> >> ... >> >> import os >> >> ... >> >> def createKML(id): >> >> ... >> >> os.mkdir("D:/path/to/mysite/templates/maps/"+str(id)) >> >> writefile = "D:/path/to/mysite/templates/maps/"+str(id)+"/"+str(id) >> >> + ".kml" >> >> f = open(writefile, 'wb') >> >> ... >> >> >> I hope this helps? >> >> >> On 14 aug, 21:55, Mike Dewhirst wrote: >> >>> On 15/08/2010 12:10am, Mark Mooij wrote: >> >> Hi all, >> >> I recently migrated from Django 1.1.1 to 1.2.1. In 1.1.1 I had an >> application which imports an external python script in which a >> directory and some files are created. Since the migrate this doesn't >> work anymore, I get a number of IOErrors because the files (and >> directory) that should be created can't be found. I've tried changing >> my directories and slashes (I'm using Windows), but no success. >> >> >>> Have you changed anything else? >> >> >>> For example, are you processing the Microsoft Windows update patches >> >>> which (for WinXP) insert extra permission steps? Have you changed your >> >>> OS from XP to Win7? >> >> >>> Can you post the source of the script? >> >> Can't I use the os python functions anymore in Django 1.2.1? In the >> documentation I've read about Managing files in models but I prefer to >> handle this through my external script. >> >> I hope anybody can help me. >> >> Thanks, >> Mark > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To
Re: Create files and directories from Django 1.2.1
On Mon, Aug 16, 2010 at 9:49 AM, Mark Mooij wrote: > Hi Doug, > > Thanks for your reply. I tried your suggestion: > .. > import createKML > .. > def createKML(id, basepath=os.getcwd()): > ... > try: > kmlpath = os.path.join(basepath, "templates") > kmlpath = os.path.join(kmlpath, "maps") > kmlpath = os.path.join(kmlpath, str(id)) > if not os.path.isdir(kmlpath): > os.mkdir(kmlpath) > writefile = os.path.join(kmlpath, (str(id)+".kml")) > > except Exception as e: > ... > > f = open(writefile, 'wb') BTW, you can: kmlpath = os.path.join(basepath, "templates", "maps", str(id)) [snip] > Also I find it strange that makedirs command rises > an error when calling from Django ("mkdir() takes exactly 1 argument > (2 given)"), but again outside Django this works just fine.. Ok, that is very suspicious. It sounds like there is another os module that you are somehow getting, rather than the Python system one. Perhaps have a look at os.__file__ to see if that is the correct version. -Doug > Hope anybody has any suggestions.. > Mark > > On 16 aug, 14:22, Doug Blank wrote: >> On Mon, Aug 16, 2010 at 7:23 AM, Mark Mooij wrote: >> > Allright I tried a couple of things with your suggested appraoch: >> >> > - I printed all the strings used to define paths and files, this all >> > seems correct. >> >> > - The os.mkdir() command is passed without raising an error, but >> > without creating the directory either, this results in an "IOError: No >> > such file or directory" because of course a file can't be created in a >> > non-existing directory. >> >> > - I tried creating files in different already existing directories and >> > this works as expected. >> >> > - I tried to use os.makedirs() instead of os.mkdir(), this raises an >> > interesting error: "mkdir() takes exactly 1 argument (2 given)", I >> > tried calling the script outside of Django and this should work the >> > way I'm using it: >> >> > kmlpath = "%s/%s" % (basepath, game_id) >> > if not os.path.isdir(kmlpath): >> > os.makedirs(kmlpath) >> >> > - I tried creating a file without any directory structure in the path, >> > the file is created in the root of the website, if I try creating a >> > directory with os.mkdir() it should take the root as well I presume, >> > but no directory is created. >> >> > It seems that Django 1.2.1 has problems calling the os.mkdir() and >> > makedirs() commands ? >> >> I doubt it. You shouldn't really do this: >> >> kmlpath = "%s/%s" % (basepath, game_id) >> >> but do this: >> >> kmlpath = os.path.join(basepath, game_id) >> >> as different OS's have different path formats. Or perhaps it is the >> kml string that you are creating that has other issues (unicode?) I >> would print out the mkdirs string to see what you are creating. >> >> HTH, >> >> -Doug >> >> >> >> > Mark >> >> > On 16 aug, 08:32, Mike Dewhirst wrote: >> >> On 16/08/2010 12:57pm, Mike Dewhirst wrote: >> >> >> > On 16/08/2010 1:06am, Mark Mooij wrote: >> >> >> Hi Mike, >> >> >> >> Thanks for your reply. I haven't changed anything else, I am >> >> >> processing the MS updates, but I don't think this is the problem, as >> >> >> this all worked fine before the migration to 1.2.1, also if I call the >> >> >> script directly (outside Django) it works fine. >> >> >> My view looks like this: >> >> >> .. >> >> >> import createKML >> >> >> .. >> >> >> def my_view(request, arg): >> >> >> ... >> >> >> createKML.createKML(arg) >> >> >> > I don't really know what is happening here that might have changed in >> >> > 1.2.1 maybe someone else can suggest something. >> >> >> > In the meantime here is suggested approach to discovering the exact >> >> > problem ... >> >> >> > import os >> >> > ... >> >> > def createKML(id, basepath="D:/path/to/mysite/templates/maps"): >> >> > ... >> >> > try: >> >> > if id: >> >> > kmlpath = "%s/%s" % (basepath
Re: Create files and directories from Django 1.2.1
On Mon, Aug 16, 2010 at 8:51 PM, Mike Dewhirst wrote: > On 16/08/2010 10:22pm, Doug Blank wrote: >> >> On Mon, Aug 16, 2010 at 7:23 AM, Mark Mooij wrote: >>> >>> Allright I tried a couple of things with your suggested appraoch: >>> > > > >> >> I doubt it. You shouldn't really do this: >> >> kmlpath = "%s/%s" % (basepath, game_id) > > I suggested this to avoid having to cast game_id as a string. I'm not all > that knowledgeable about the upcoming Python 3 tsunami but I figure there > will be less hassle if I use the inbuilt string formatting in %s. At least I > hope so! > >> >> but do this: >> >> kmlpath = os.path.join(basepath, game_id) > > Now I have a question - more Python than Django. os.path.join is very clever > but in Windows we have multiple approaches. One is to simply use forward > slashes and rely on the infrastructure to know what to do and the other is > to be agnostically cross-platform with os.path.sep and os.path.join. We can > also use r'raw\backslashes' and u'C:\\escaped\\backslashes' > > I have come unstuck using all of them at one time or another. I can't quite > put my finger on it at the moment but I have also seen things like > 'C:\path\to/some/file.txt' arising from combinations of the approaches and > surprisingly being successful. > > So my question is, has anyone written a dissertation on the best approaches > for particular circumstances? Always use os.path.join and you will (almost) never go wrong, and will work cross-platform. If you need to make something into a string, use: kmlpath = os.path.join(basepath, str(item1), str(game_id), str(subthing)) One caveat is that the encoding of a string versus the file system encoding can differ, so you might need something different than str or unicode. But that doesn't sound like it is your original problem. -Doug > Mike > >> >> as different OS's have different path formats. Or perhaps it is the >> kml string that you are creating that has other issues (unicode?) I >> would print out the mkdirs string to see what you are creating. >> >> HTH, >> >> -Doug >> >>> Mark >>> >>> On 16 aug, 08:32, Mike Dewhirst wrote: >>>> >>>> On 16/08/2010 12:57pm, Mike Dewhirst wrote: >>>> >>>> >>>> >>>> >>>> >>>>> On 16/08/2010 1:06am, Mark Mooij wrote: >>>>>> >>>>>> Hi Mike, >>>> >>>>>> Thanks for your reply. I haven't changed anything else, I am >>>>>> processing the MS updates, but I don't think this is the problem, as >>>>>> this all worked fine before the migration to 1.2.1, also if I call the >>>>>> script directly (outside Django) it works fine. >>>>>> My view looks like this: >>>>>> .. >>>>>> import createKML >>>>>> .. >>>>>> def my_view(request, arg): >>>>>> ... >>>>>> createKML.createKML(arg) >>>> >>>>> I don't really know what is happening here that might have changed in >>>>> 1.2.1 maybe someone else can suggest something. >>>> >>>>> In the meantime here is suggested approach to discovering the exact >>>>> problem ... >>>> >>>>> import os >>>>> ... >>>>> def createKML(id, basepath="D:/path/to/mysite/templates/maps"): >>>>> ... >>>>> try: >>>>> if id: >>>>> kmlpath = "%s/%s" % (basepath, str(id)) >>>> >>>> actually .. kmlpath = "%s/%s" % (basepath, id) >>>> >>>>> if not os.path.isdir(kmlpath): >>>>> os.makedirs(kmlpath) >>>>> writefile = "%s/%s" % (kmlpath, str(id)) >>>> >>>> actually .. writefile = "%s/%s" % (kmlpath, id) >>>> >>>> >>>> >>>>> f = open(writefile, 'wb') >>>>> else: >>>>> raise >>>>> except Exception as e: >>>>> print("id = %s\nbasepath = %s\n%s" % (id, basepath, e) >>>>> ... >>>> >>>>> mike >>>> >>>>>> ... >>>> >>>>>> In the createKML.py the problem arises in: >>>>>> ... >>>>>> import os >>>>>> ... >>>>>> def c
Re: error in the example
On Wed, Dec 30, 2009 at 6:24 PM, gintare wrote: > Hello, > > In short > OS Debian Gnu Linux Lenny > Python 2.5 > Installed Django-1.1.1.tar.gz, > /usr/lib/python-django/Django-1.1.1/docs/intro/tutorial01.txt > > command python manage.py sql polls > gives error: IndentationError: unexpected indent > in File "/mano/mysite3/../mysite3/polls/models.py", > line 5 > class Poll (models.Model): > > What is wrong with syntax in this line "class Poll (models.Model):" ? > > Looks like you have a space or two in front of the word "class". Spaces are meaningful in Python. -Doug > > --- > trying according example: > /usr/lib/python-django/Django-1.1.1/docs/intro/tutorial01.txt > > the sequence of the commands, which went fluently was: > > `django-admin.py startproject mysite3' > (i have to make adjustments to settings.py choosing Sqlite3 and > creating database) > > python manage.py runserver > python manage.py syncdb > python manage.py startapp polls > > creates folder polls with file models.py into which i have to paste > code from tutorial01.txt > . >class Poll(models.Model): >question = models.CharField(max_length=200) > . > > the next command python manage.py sql polls > gives error: IndentationError: unexpected indent > in File "/mano/mysite3/../mysite3/polls/models.py", > line 5 > class Poll (models.Model): > > > -- > The whole error report: > > working:/mano/mysite3# python manage.py sql polls > Traceback (most recent call last): > File "manage.py", line 11, in >execute_manager(settings) > File "/usr/lib/python2.5/site-packages/django/core/management/ > __init__.py", line 362, in execute_manager >utility.execute() > File "/usr/lib/python2.5/site-packages/django/core/management/ > __init__.py", line 303, in execute >self.fetch_command(subcommand).run_from_argv(self.argv) > File "/usr/lib/python2.5/site-packages/django/core/management/ > base.py", line 195, in run_from_argv >self.execute(*args, **options.__dict__) > File "/usr/lib/python2.5/site-packages/django/core/management/ > base.py", line 221, in execute >self.validate() > File "/usr/lib/python2.5/site-packages/django/core/management/ > base.py", line 249, in validate >num_errors = get_validation_errors(s, app) > File "/usr/lib/python2.5/site-packages/django/core/management/ > validation.py", line 28, in get_validation_errors >for (app_name, error) in get_app_errors().items(): > File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", > line 131, in get_app_errors >self._populate() > File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", > line 58, in _populate >self.load_app(app_name, True) > File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", > line 74, in load_app >models = import_module('.models', app_name) > File "/usr/lib/python2.5/site-packages/django/utils/importlib.py", > line 35, in import_module >__import__(name) > File "/mano/mysite3/../mysite3/polls/models.py", line 5 >class Poll(models.Model): >^ > IndentationError: unexpected indent > > regards, > gintare > g.statk...@gmail.com > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Foreign Key Across Database in Django multidb
On Wed, Dec 30, 2009 at 11:54 PM, malik wrote: > I heard that that multi DB support is now available in Django.Can some > one tell me whether it is possible to have foreign Key across > Databases. > > Please see this thread: http://groups.google.com/group/django-users/browse_thread/thread/74bcd1afdeb2f0?hl=en -Doug > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: search db & if sequences
On Sun, Jan 24, 2010 at 1:05 PM, mehdi0016 wrote: > hi > i'm new with python and django and i work on search page of my site. > in my html search form, user can choose table(or field) which want to > search. in server-side i use sequences of 'if' to find chosen table(or > field) and related django model. > ... > #here i assume searched fields have the same name('title') > q = requst.GET['query'] > tbl = requst.GET['table'] > if tbl == 'Book': >result = Book.objects.filter(title__icontains=q) > if tbl == 'Author': >result = Author.objects.filter(title__icontains=q) > ... > now is there any way to reduce or eliminate 'if' sequences? > i test this and it works: > ... > tbl = eval(requst.GET['table']) > ... > but i'm not sure that is best way? > Not only is that not the best way, but may well be the worst. Consider: >>> print requst.GET['table'] "select os; os.system('rm -rf /')" You should never eval() data from a user. What is surely better might be something like: tables = {"Author": Author, "Book": Book,} if requst.GET['table'] in tables: matches = tables[requst.GET['table']].objects.filter(title__icontains=requst.GET['query']) You can make this more sophisticated and even allow them to select the field, or just search them all. Hope that helps! -Doug > thanks > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Reg. Improving Django response time (i.e throughput)
On Thu, Jan 28, 2010 at 3:53 PM, Saravanan wrote: > I have Django installed in SUSE Linux with postgres. It is working > great. Kudos > I have an issue. I have total database structure containing more than > 100million rows. I have partitions and indexes. It is taking plenty of > time to load Django initially. I meant in minutes. I can take a coffee > break and a short workout schedule. Is there any ideas/links and other > resources to speed up the response time (Throughput) > For you information, I am looking at Cache and DB tunning. Any other > tricks with Django, anything you are familiar with it? > The first thing you should look at is what queries are being run. You might be running too many queries, or it might be that you need to add some indexes to your database. Try looking at django-toolbar. The fact that you have 100 million rows shouldn't really matter. -Doug > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Error in Brand New Django 1.4.1 released Yesterday
On Tue, Jul 31, 2012 at 3:15 AM, JJ Zolper wrote: > Hello all, > > I didn't realize a new release was put out until something weird happened. > > I'm installing DJ on my production server now and when I was downloading a > copy from https://www.djangoproject.com/download/ On that same page (option number 2) are a couple of links for the "Latest Development Version". The zip file is named: django-django-1.4-616-g8d3e501.zip but indeed the version in the zip file is: VERSION = (1, 5, 0, 'alpha', 0) -Doug > and installed it and went into python: > import django print django.get_version() > > I didn't see 1.4.1 I saw 1.5 > > Sure it's probably a minor configuration fix to change it to 1.4.1 but I was > really worried that I did something wrong. > > Maybe someone else can repeat this and see if I made a mistake or that > indeed something is wrong with the brand new release? > > Cheers to all! > > JJ > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/sN647bh67AAJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Switching settings, in a non-webapp?
Django users, We are using Django for two purposes: its ORM for a single-user desktop app, and for a regular Django webapp. For the desktop ORM, is there a way to switch between different settings.py? For example, we'd like to be able to load the default settings, but then later unload everything, and load different default settings. Looking for something like unloading/reloading all of the relevant sys.modules. We're using python3 if that matters. Thanks for any suggestions/pointers! -Doug -- 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/CAAusYCjPN3t61ku93rqhZxmV2eoLp90ddxPyqeTcEUQbx6B7bA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: Switching settings, in a non-webapp?
On Mon, May 11, 2015 at 4:44 PM, David Sieger wrote: > If restarting the whole http service is an option, you could think about > a setting up a separate service that is capable of stopping/starting your > http service. You could then order this new service from within the http > service to restart the http service with a different settings module. (I'm > thinking ZeroMQ, but there are definitely other ways to implement this.) > Thanks, but I think you have misunderstood... there is no http service... this is not a web app... only Python is involved. I need to reset the Django initialization in a Python session. -Doug > > David > > > Am 11.05.2015 um 18:17 schrieb Doug Blank: > > Django users, > > We are using Django for two purposes: its ORM for a single-user desktop > app, and for a regular Django webapp. > > For the desktop ORM, is there a way to switch between different > settings.py? For example, we'd like to be able to load the default > settings, but then later unload everything, and load different default > settings. > > Looking for something like unloading/reloading all of the relevant > sys.modules. > > We're using python3 if that matters. > > Thanks for any suggestions/pointers! > > -Doug > -- > 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/CAAusYCjPN3t61ku93rqhZxmV2eoLp90ddxPyqeTcEUQbx6B7bA%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAAusYCjPN3t61ku93rqhZxmV2eoLp90ddxPyqeTcEUQbx6B7bA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > 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/555114C7.1060306%40conceal.at > <https://groups.google.com/d/msgid/django-users/555114C7.1060306%40conceal.at?utm_medium=email&utm_source=footer> > . > 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/CAAusYCiD9PkB0_aO4bt2n8%2Bcp71KXog6%2B5UJh0fksAHCA4n8OA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: Switching settings, in a non-webapp?
This ended up being just a simple Python3 issue, and the solution below works fine, as far as I can tell so far. I post it here in case anyone finds it useful: I defined a "modules checkpoint" to which I can roll back to: >>> from modules_checkpoint import ModulesCheckpoint >>> checkpoint = ModulesCheckpoint() >>> from gramps.webapp.databases.database1 import database >>> database.dji.Person.all().count() 4 >>> checkpoint.reset() >>> from gramps.webapp.databases.database2 import database >>> database.dji.Person.all().count() 0 >>> checkpoint.reset() >>> from gramps.webapp.databases.database1 import database >>> database.dji.Person.all().count() 4 (showing the different counts in two different Django databases, defined in different settings.py). Code is below. -Doug # modules_checkpoint.py import sys class ModulesCheckpoint(object): def __init__(self): self.original = sys.modules.copy() def reset(self): # clear modules: for key in list(sys.modules.keys()): del(sys.modules[key]) # load previous: for key in self.original: sys.modules[key] = self.original[key] On Mon, May 11, 2015 at 12:17 PM, Doug Blank wrote: > Django users, > > We are using Django for two purposes: its ORM for a single-user desktop > app, and for a regular Django webapp. > > For the desktop ORM, is there a way to switch between different > settings.py? For example, we'd like to be able to load the default > settings, but then later unload everything, and load different default > settings. > > Looking for something like unloading/reloading all of the relevant > sys.modules. > > We're using python3 if that matters. > > Thanks for any suggestions/pointers! > > -Doug > -- 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/CAAusYCjbrM%2BWbOrfP0_3uC-QPDXtwMELnFdZLNdABhX97QmYqw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: Trying to run jobs in the background using multiprocessing
On Wed, Nov 27, 2013 at 2:58 PM, Pau Creixell wrote: > Hi there! > > I am trying to run jobs in the background using the following (minimal) > code: > > Under views.py: > > def submit(request): >model = MyModel() >model.RunInAThread(ReferenceSeparator, MutationSeparator) > > Under models.py: > > class MyModel(models.Model): >def RunInAThread(self, ReferenceSeparator, MutationSeparator): >print 1 >Thread = multiprocessing.Process(target = self.Run, args=(self, > ReferenceSeparator, MutationSeparator)) I suspect that if you are setting the target to be self.Run, then the args should not include self: Thread = multiprocessing.Process(target = self.Run, args=(ReferenceSeparator, MutationSeparator)) -Doug >print 2 >Thread.daemon = True >print 3 >Thread.start() >print 4 > >def Run(self, ReferenceSeparator, MutationSeparator): >print 5 > > > But I get the error below and it only print 1, 2, 3 and 4, but not 5: > > TypeError at /submit > 'str' object is not callable > ... > Exception Location: /sw/lib/python2.6/multiprocessing/forking.py in > __init__, line 98 > ... > > I have tried in many different ways (e.g. Daemon = False, without "self" as > an "args"), but don't really understand why this would not work, unless > Django doesn't permit multiprocessing (which some people seem to suggest > online). > Any help will be highly appreciated. > > Best wishes, > Pau > > -- > 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/27f043ea-d59d-42ee-9529-0bf86ade31b0%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. -- 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/CAAusYCiekn%2BSzJ91puNKJk75HuGjWwiTph-svERB7Be_gxoisw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Need some advise on how to use a RESTful API
On Fri, Dec 20, 2013 at 11:45 AM, Mario Osorio wrote: > I need to work on a django app that will eventually benefit from a RESTful > API so it can be used from any web browser as well a from apps writtens for > iOS and Android. > > Being so new to django development I don't even have any ideas on how to use > a RESTful API, for example, > > Should I begin the development use a RESTful API, or should I add ir when > needed? ( it IS going to be used anyways) > Of course I understand I'm adding another layer of work to the final product > but, can someone please explain how bad deep and how complicated this layer > might be? > I've found a couple of online tutorials and I'll soon be working on them, > but I honestly think there is nothing quite like reading the opinions of > different people as to what the workflow with a RESTful API in django > implies. I think a simple overview of the general workflow will answer most > of the questions I have in mind right now. > > Thanks a lot in advanced! I'm not an expert, but I just happen to be working on something similar. I already had the URLs set up so that the HTML was being rendered, edited, saved, etc. So, then I wanted to add a RESTful API, mirroring the same URLs. A regular URL looks like: http://localhost:8000/person/764526236 I decided to make the REST API look like: http://localhost:8000/person/764526236/?format=json That was as easy as something like this (person is the view, 764526236 is the handle): try: obj = Person.objects.get(handle=handle) except: raise Http404(_("Requested %s does not exist.") % view) if not check_access(request, context, obj, act): raise Http404(_("Requested %s does not exist.") % view) if "format" in request.GET: if request.GET["format"] == "json": person = db.get_person_from_handle(obj.handle) content = str(person.to_struct()) response = HttpResponse(content, mimetype="application/json") return response The 'if "format"' handles the different formats. Hope that helps (or someone can point us both in a better direction). -Doug > -- > 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/cc10edc2-e978-461e-986f-43dd73eea63e%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. -- 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/CAAusYCiW69LsCf%3DAfGYJD4oZPpJziJwAaFKtiU%3DxnDb05nxajA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.