get object qs with generic relation
Dear Django users, I have a tagging system that uses a GenericForeignKey to assign tags to generic objects. I would like to write a function that given a model and a list of tags returns a QuerySet containing all instances of that model that have been given those tags. I'm not a huge fan of django-tagging's solution (get_intersection_by_model): http://code.google.com/p/django-tagging/source/browse/trunk/tagging/models.py Any elegant solutions out there? Thanks for the help, Andrew -- 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: script adding FileField, no attribute chunks
I'm trying to add a bunch of files from disk into my django database. Here's the helper function I've written so far: def django_file(path, field_name, content_type): # adapted from here: http://groups.google.com/group/django-users/browse_thread/thread/834f988876ff3c45/ from django.core.files.uploadedfile import InMemoryUploadedFile f = open(path) return InMemoryUploadedFile( file=f, field_name=field_name, name=file.name, content_type=content_type, size=os.path.getsize(path), charset=None) I'm calling it like so: django_file("path-to-jpg-file", field_name="image", content_type="image/jpeg") Here's the error I'm getting: Traceback (most recent call last): File "", line 1, in File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 49, in import_instances_folder f = django_file(xml_files[0], field_name="xml_file", content_type="text/xml") File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 70, in django_file charset=None) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 90, in __init__ super(InMemoryUploadedFile, self).__init__(file, name, content_type, size, charset) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 30, in __init__ super(UploadedFile, self).__init__(file, name) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/base.py", line 17, in __init__ self.name = name File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 46, in _set_name name = os.path.basename(name) File "/home/amarder/Documents/environments/nmis/lib/python2.6/ posixpath.py", line 111, in basename i = p.rfind('/') + 1 AttributeError: 'member_descriptor' object has no attribute 'rfind' Any suggestions? Andrew On Nov 16, 4:36 pm, Mitch Anderson wrote: > On Tue, Nov 16, 2010 at 3:28 AM, Tom Evans wrote: > > Django doesn't want a python file or text for a django file field, it > > wants a django.core.files.File. I find the easiest one to use is the > > InMemoryUploadedFile. Here is a snippet I use for fetching an image > > from the web, and creating a django.core.files.File object that can be > > assigned to a FileField or ImageField on a model: > > > h = httplib2.Http() > > req, content = h.request(uri) > > if req['status'] != '200': > > print u'Failed to fetch image from %s' % uri > > return None > > > import cStringIO > > from django.core.files.uploadedfile import InMemoryUploadedFile > > out = cStringIO.StringIO() > > out.write(content) > > return InMemoryUploadedFile( > > file=out, > > field_name=field, > > name=name, > > content_type=req['content-type'], > > size=out.tell(), > > charset=None) > > > field should be the name of the field on the model, name should be the > > file name of the resource. > > > There may be neater ways of doing this, but this keeps it in memory > > until django saves it to the upload_to location specified on the > > model, and avoids writing it to disk only for django to write it to > > disk again. > > > Cheers > > > Tom > > Awesome that worked perfectly! Thanks 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.
Re: script adding FileField, no attribute chunks
Awesome! Thanks so much for the help. Andrew On Wed, 2010-12-01 at 12:32 +, Tom Evans wrote: > On Wed, Dec 1, 2010 at 12:23 PM, Andrew Marder > wrote: > > I'm trying to add a bunch of files from disk into my django database. > > Here's the helper function I've written so far: > > > > def django_file(path, field_name, content_type): > ># adapted from here: > > http://groups.google.com/group/django-users/browse_thread/thread/834f988876ff3c45/ > >from django.core.files.uploadedfile import InMemoryUploadedFile > >f = open(path) > >return InMemoryUploadedFile( > >file=f, > >field_name=field_name, > >name=file.name, > > This should be 'f.name', not 'file.name'. file is a built in class in > python, and file.name is a member of that class, not your file name. > > Cheers > > Tom > > >content_type=content_type, > >size=os.path.getsize(path), > >charset=None) > > > > > > > I'm calling it like so: > > django_file("path-to-jpg-file", field_name="image", > > content_type="image/jpeg") > > > > Here's the error I'm getting: > > Traceback (most recent call last): > > File "", line 1, in > > File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 49, > > in import_instances_folder > >f = django_file(xml_files[0], field_name="xml_file", > > content_type="text/xml") > > File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 70, > > in django_file > >charset=None) > > File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- > > packages/django/core/files/uploadedfile.py", line 90, in __init__ > >super(InMemoryUploadedFile, self).__init__(file, name, > > content_type, size, charset) > > File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- > > packages/django/core/files/uploadedfile.py", line 30, in __init__ > >super(UploadedFile, self).__init__(file, name) > > File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- > > packages/django/core/files/base.py", line 17, in __init__ > >self.name = name > > File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- > > packages/django/core/files/uploadedfile.py", line 46, in _set_name > >name = os.path.basename(name) > > File "/home/amarder/Documents/environments/nmis/lib/python2.6/ > > posixpath.py", line 111, in basename > >i = p.rfind('/') + 1 > > AttributeError: 'member_descriptor' object has no attribute 'rfind' > > > > Any suggestions? > > > > Andrew > > > > On Nov 16, 4:36 pm, Mitch Anderson wrote: > >> On Tue, Nov 16, 2010 at 3:28 AM, Tom Evans > >> wrote: > >> > Django doesn't want a python file or text for a django file field, it > >> > wants a django.core.files.File. I find the easiest one to use is the > >> > InMemoryUploadedFile. Here is a snippet I use for fetching an image > >> > from the web, and creating a django.core.files.File object that can be > >> > assigned to a FileField or ImageField on a model: > >> > >> > h = httplib2.Http() > >> > req, content = h.request(uri) > >> > if req['status'] != '200': > >> >print u'Failed to fetch image from %s' % uri > >> >return None > >> > >> > import cStringIO > >> > from django.core.files.uploadedfile import InMemoryUploadedFile > >> > out = cStringIO.StringIO() > >> > out.write(content) > >> > return InMemoryUploadedFile( > >> > file=out, > >> > field_name=field, > >> > name=name, > >> > content_type=req['content-type'], > >> > size=out.tell(), > >> > charset=None) > >> > >> > field should be the name of the field on the model, name should be the > >> > file name of the resource. > >> > >> > There may be neater ways of doing this, but this keeps it in memory > >> > until django saves it to the upload_to location specified on the > >> > model, and avoids writing it to disk only for django to write it to > >> > disk again. > >> > >> > Cheers > >> > >> > Tom > >> > >> Awesome that worked perfectly! Thanks 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.
lowercase alphanumeric usernames
Dear Django Users, I want to restrict usernames to be lowercase alphanumeric characters like this: def clean_username(username): return re.sub(r'\W', '', username).lower() Now, I'm not sure where is best to do this. I thought it might be clever to do this using middleware like this: def process_view(self, request, view_func, view_args, view_kwargs): if 'username' in request.POST: request.POST['username'] = clean_username(request.POST['username']) But, then I saw this note: https://docs.djangoproject.com/en/dev/topics/http/middleware/#process-view I've also seen the case insensitive authentication backend approach here: http://groups.google.com/group/django-users/browse_thread/thread/2967916ed3c77726/8f1fe50224b4e6ad But, it seems like I would still be able to create two users one with username 'admin' and another with username 'Admin'. So, I'll open this up to the Django pros, any suggestions on how to enforce that all my users have lowercase alphanumeric usernames? Thanks, Andrew -- 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.
using signals to add users to a group
Dear Django Users, I want to add users to a particular group based on the domain of their email address. I set up some code in models.py to do this and it seems to work. But, when I go to test the code things don't work out so smoothly. I think in the test environment the objects I try to create in models.py may not be inserted in the test database. Any ideas for good work arounds? https://github.com/mvpdev/nmis/blob/feature%2Fdj13/user_management/models.py#L29 https://github.com/mvpdev/nmis/blob/feature%2Fdj13/user_management/tests.py#L35 Andrew -- 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 signals to add users to a group
Hi Shawn, I've pasted the traceback below, I need to write some more tests to give you better information. Andrew $ python manage.py test user_management Creating test database for alias 'default'... ..F == FAIL: test_user_added_to_appropriate_group (user_management.tests.SignalTest) -- Traceback (most recent call last): File "/home/amarder/Documents/sql_mangrove/nmis/user_management/ tests.py", line 39, in test_user_added_to_appropriate_group self.assertTrue(technical_assistants in allen.groups.all()) AssertionError: False is not true -- Ran 3 tests in 1.319s FAILED (failures=1) Destroying test database for alias 'default'... On Jul 18, 1:02 pm, Shawn Milochik wrote: > What traceback do you get? -- 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.
Mongo - testing
Has anyone had any luck setting up testing databases with mongo? Right now I'm using pymongo in a single app, and I thought it would be cool if in that app I could see if my code was being tested and in that case I could use a different database. Problem is there doesn't seem to be an environment variable in Django that will tell me whether my code is being tested. Thanks for the help, Andrew http://groups.google.com/group/django-users/browse_thread/thread/3a5a756782d24b5e?pli=1 -- 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: Mongo - testing
Thanks Javier and Konrad! I searched github a bit and it looks like django-mongokit has done some work to solve this problem. I haven't used it yet, but it looks promising. Andrew http://www.peterbe.com/plog/how-and-why-to-use-django-mongokit On Jan 21, 3:59 am, Konrad Delong wrote: > On 20 January 2011 22:26, Andrew Marder wrote: > > > Has anyone had any luck setting up testing databases with mongo? Right > > now I'm using pymongo in a single app, and I thought it would be cool > > if in that app I could see if my code was being tested and in that > > case I could use a different database. Problem is there doesn't seem > > to be an environment variable in Django that will tell me whether my > > code is being tested. > > In one of the projects I worked on, we used a separate settings file > for testing. You could implement your own flag in there. > > cheers, > Konrad -- 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.