Re: Complex Lookups
> > But be sure to fix the typo in x first! > > >>> x = [ {'id': 1, 'title': 'test1'}, {'id': 2, 'test2'} ] > File "", line 1 > x = [ {'id': 1, 'title': 'test1'}, {'id': 2, 'test2'} ] > ^ > SyntaxError: invalid syntax > Ha, whoops. I don't think I've ever typed a working line of code in my life the first time! I just got this working and it's great. I feel like I owe you all a steak dinner or something. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Complex Lookups
> Try this: > > x = [ {'id': 1, 'title': 'test1'}, {'id': 2, 'test2'} ] > if x: > d = x[0] > q = Q(id=d['id']) & Q(title=d['title']) > for d in x[1:]: > q = q | (Q(id=d['id']) & Q(title=d['title'])) > query = MyModel.objects.filter(q) Hey, that makes sense. I do appreciate the speedy help! --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Complex Lookups
I've spent a little too much time on this, but before I decided to make a custom sql query, I wanted to see if anyone could provide any assistance. It is really a python question, and the problem I'm having is I'm not an expert yet with python. First, I have a list of dictionaries. [ {'id': 1, 'title': 'test1'}, {'id': 2, 'test2'} ] I want to do a SQL query like this: SELECT * FROM mytable WHERE (id = 1 AND title = 'test1') OR (id = 2 AND title = 'test2') I know I can use the Q object, like: MyModel.objects.filter(Q(id=1) & Q(title='test1') | Q(id=2) & Q(title='test2')) My question is how can I convert my list to those Q objects? The list may have 2 dictionaries in it like this, or it may have 20. My hangup is getting things into separate Q objects with the correct operator in between them. There could also be a better way that I don't know of! 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: upload_to with username
> That line is executed exactly once, at the time the file is imported. So > it is not dynamic in any way. > > (I seem to be saying that at least every other day here. It's normal > Python behaviour, people. Please stop hoping it were otherwise!) Thanks for the help. I didn't call it dynamic, though. What are you referring to? I'll look through the fields.py file and see what I can do. I'm pretty new to Django and even Python, so I'm guessing I won't have much luck ;) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
upload_to with username
I'm wanting to have upload_to for my file so it uploads to a different directory for each user. This works when I use it with a newform, but when I try to use the Django admin, it puts None as the username (I want to base it on whatever username I select for "user"). I'm using some middleware for the threadlocals to get the current user. Any ideas on how to make this work? from project.middleware import threadlocals class File(models.Model): name = models.CharField(maxlength=100) file = models.FileField(upload_to=str(threadlocals.get_current_user()) + '/ files') user = models.ForeignKey(User) def save(self): if not self.user: self.user = threadlocals.get_current_user() super(File,self).save() --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Restricting Access to Users
Sweet, thanks for your help, Sam. I'll be reading about this the rest of the afternoon. On Mar 13, 10:54 am, "Sam" <[EMAIL PROTECTED]> wrote: > It's up to your code inside the middleware to restrict or allow the > access. > > For example, you could check the request.META['PATH_INFO'] for zones > where the middleware shouldn't do anything. > > On Mar 13, 4:47 pm, "Stephen Mizell" <[EMAIL PROTECTED]> wrote: > > > > You should go with a MiddleWare. > > > Will this work for the Django admin as well? I don't think I > > specified in my post that I was wanting to restrict the access in the > > admin for certain users, but still make each Weblog public. Sorry for > > the confusion. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Restricting Access to Users
> You should go with a MiddleWare. Will this work for the Django admin as well? I don't think I specified in my post that I was wanting to restrict the access in the admin for certain users, but still make each Weblog public. Sorry for the confusion. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Restricting Access to Users
I'm guessing this question has been answered somewhere, but I am having a hard time finding an answer. I'll explain my question by way of an example. Let's say I have a Weblog model which is related to a Post model. I want many Weblogs on the site, and each Weblog can have many Posts. My situation is, I want to be able to restrict access to certain Weblogs (and everything associated with that Weblog such as Posts) for certain people. What is the best method to do this? Would I use the Site framework for this, and if so, is there a good tutorial for this? I don't really want to make a new app for each Weblog, but if that's the best way I'm down with it. If this has been posted somewhere, could someone kindly point me in that direction? Thanks for your help. Stephen --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Filter "Dates"
> Try doing the filtering first (since filter() returns a QuerySet) and > then calling dates() on the result. So > > Model.objects.filter().dates() Worked perfectly. I would have bet money I had tried that earlier, but it looks like I would have lost that bet. Thanks for your help Malcolm! Stephen Mizell --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Filter "Dates"
I'm trying my hardest to make an archive list on our website. I can pull them up fine with Model.objects.dates('pub_date','month') but I can't use .filter() on them for some reason. We add things to the database sometimes with a pub_date > today's date so things are automatically published in the future. Is there a way to use something to filter these out? Model.objects.dates('pub_date','month', order='DESC').filter(pub_date__month__lte=date.month, pub_date__year__lte=date.year) Note: To explain the "lte" on month and year, we want new articles to post the first day of each month. This is why I don't use now(). Thanks! Stephen Mizell --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Convert From Restructured Text to HTML
That's exactly what happened, but strange thing was it worked just fine using the filter. I even tried making some SQL queries to fix them for me, but that didn't work. Oh well, it's over now ;) On 1/10/07, James Bennett <[EMAIL PROTECTED]> wrote: > > > On 1/9/07, Stephen Mizell <[EMAIL PROTECTED]> wrote: > > This started throwing all kinds of errors at me about encodings, such > > as: > > 'latin-1' codec can't encode characters in position 19-20: ordinal not > > in range(256) > > Almost every time I've seen this (not quite every time, but almost > every time), it's been because someone copy/pasted from an MS Word > document which contained "smart quotes". > > -- > "May the forces of evil become confused on the way to your house." > -- George Carlin > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Convert From Restructured Text to HTML
Well, I gave up on the automation. I couldn't get restructuredtext to work right in my views, so I swallowed my pride and went into copy/paste mode. I simple did this: {% load markup %} {% for post in posts %} {{ post.id }} {{ post.body|restructuredtext|escape }} {% endfor %} I edited each and every post and pasted the stuff from that page above. This is for anyone comes across the same issue and can't fix it like I couldn't. My fingers hurt now. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Convert From Restructured Text to HTML
So it seems the issue is with the restructured text. When using the restructured text filter on a template, it works just fine. When using restructured text in a view, it gives me tons of errors. Any ideas? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Convert From Restructured Text to HTML
In our database we have a TextField that stores the body of our posts in RestructuredText. I've recently added TinyMCE to the Django Admin, so I'm wanting to convert this field from rst to plain old html. I wrote a little thing of code to do this with Django. def convert_post(request): posts = Post.objects.all() for post in posts: post.body = restructuredtext(post.body) post.save() This started throwing all kinds of errors at me about encodings, such as: 'latin-1' codec can't encode characters in position 19-20: ordinal not in range(256) One time it said 'latin-1', next time it said 'ascii', and who knows what else it will come up with as the encoding. I guess this means we have some characters mysql isn't liking, but I'm clueless on how to easily convert it without fixing those errors one by one as I find them. Any ideas? Any way to convert this works for me... PHP, perl, python, Django, what-have-ya. Just something quick and painless so I can get my employees a WYSIWYG. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Error on File Upload
We are using the admin for one our projects. In our model, we have: class Resource(models.Model): # ... filename = models.FileField(unique=True, upload_to="tick/resource", null=True, blank=True) We were just uploading an excel spreadsheet, which we've had no trouble doing in the past, and got an error: ProgrammingError at /resource/592/ (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''content': '\\'\\xd0\\xcf\\x11\\xe0\\xa1\\xb1Z\\xe100000000000' at line 1") Request Method: POST Request URL:http://server/resource/592/ Exception Type: ProgrammingError Exception Value:(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''content': '\\'\\xd0\\xcf\\x11\\xe0\\xa1\\xb1Z\\xe100000000000' at line 1") Exception Location: C:\Python24\lib\site-packages\MySQLdb\connections.py in defaulterrorhandler, line 35 I'm really quite new to Django and have no clue on where to go for this. Could it be because of the apostrophes? If so, how could I correct that? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Help with creating queryset
That's awesome! Worked perfectly and is so much easier to manage. Thanks for the quick help, I really appreciate it. On Dec 8, 9:28 am, Wolfram Kriesing <[EMAIL PROTECTED]> wrote: > > What is the best way to create a queryset for this without doing tons > > of if statements, like: > > > if request.GET['first_id'] > > if request.GET['second_id'] > > if request.GET['third_id'] > > queryset = > > Resource.objects.filter(first__id__exact=request.GET['first_id'], > > second__id__exact=request.GET['second_id'], > > third__id__exact=request.GET['third_id']) > > else > > queryset = > > Resource.objects.filter(first__id__exact=request.GET['first_id'], > > second__id__exact=request.GET['second_id']) > > else > > queryset = > > Resource.objects.filter(first__id__exact=request.GET['first_id']) > > elseif > > #... so onYou might try this: > > kwargs = {} > for key in ['first_id', 'second_id', 'third_id']: > if request.GET[key]: > kwargs[key] = request.GET[key] > Resource.objects.filter(**kwargs) > > (i didnt test it, but thats the idea i guess ...) > > > That just seems like the worst way to do it. Can I pass a dictionary > > to it somehow? What's the best way to do something like this? > > > Hope this wasn't extremely confusing. Let me know if you need any > > other information from me. > > > Thanks a lot, > > Stephen Mizell --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Help with creating queryset
I'm rather new to Django but am getting pretty competent doing most of the basic things. This is not so much a problem, more so trying to find the best way to do something. We are creating a website for searching our resources here in the office. We have an advanced search that allows us to search information that is related to the "Resource" being searched. The advanced search allows for a keyword and then 3 select fields that are information from other tables. A simple version of my model is below. class Resource(models.Model): first = models.ManyToManyField(First) second = models.ManyToManyField(Second) third = models.ManyToManyField(Third) So now when I search from our advanced form, the URL looks something like: http://site/search/?keyword=stuff_id=1_id=2 What is the best way to create a queryset for this without doing tons of if statements, like: if request.GET['first_id'] if request.GET['second_id'] if request.GET['third_id'] queryset = Resource.objects.filter(first__id__exact=request.GET['first_id'], second__id__exact=request.GET['second_id'], third__id__exact=request.GET['third_id']) else queryset = Resource.objects.filter(first__id__exact=request.GET['first_id'], second__id__exact=request.GET['second_id']) else queryset = Resource.objects.filter(first__id__exact=request.GET['first_id']) elseif #... so on That just seems like the worst way to do it. Can I pass a dictionary to it somehow? What's the best way to do something like this? Hope this wasn't extremely confusing. Let me know if you need any other information from me. Thanks a lot, Stephen Mizell --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---