Re: export .csv file from database
Two things: First, Ms Excel sucks. From that attached screenshot it's obvious that it thought it was a space separated file. When actually using MS Office, I recommend that you add `quoting=csv.QUOTE_ALL` to all csv writers. Second, the HttpResponse object is file-like. You don't need to jump through all the hoops. And MS Office is going to ignore your encoding anyway, so there's no point in trying to fix it. On Jun 17, 2013, at 9:44 AM, roopasingh...@gmail.com wrote: > views.py > > @login_required > def export_csv(request): > user = request.user > # heading columns > headerrow = ['Filename', 'Description','Notes other','Reporter Name'] > allrows = [] > today = datetime.datetime.today() # default to show today > reports = Report.objects.filter(user=user) > reports = reports.filter(created_date_time__year=today.year) > for report in reports: > row = [] > row.append(report.report_number); > row.append(report.report_description); > row.append(report.notes_other); > row.append(report.reporter_name); > allrows.append(row) > # setup CSV export > response = HttpResponse(mimetype='text/csv') > filename = "%s-%s.csv" % ('Daily-reports', > datetime.datetime.now().strftime('%Y-%m-%d')) > response['Content-Disposition'] = 'attachment; filename=%s' % filename > writer = UnicodeWriter(response, encoding='utf-8') > writer.writerow(headerrow) > for writerow in allrows: > writer.writerow(writerow) > return response > > csv_unicode.py > > import csv, codecs, cStringIO > class UnicodeWriter: >def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): > # Redirect output to a queue > self.queue = cStringIO.StringIO() > self.writer = csv.writer(self.queue, dialect=dialect, **kwds) > self.stream = f > self.encoder = codecs.getincrementalencoder(encoding)() > > def writerow(self, row): > self.writer.writerow([unicode(s).encode("utf-8") for s in row]) > # Fetch UTF-8 output from the queue ... > data = self.queue.getvalue() > data = data.decode("utf-8") > # ... and reencode it into the target encoding > data = self.encoder.encode(data) > # write to the target stream > self.stream.write(data) > # empty queue > self.queue.truncate(0) > > def writerows(self, rows): > for row in rows: > self.writerow(row) > > The above code is exporting the database data into .csv file. > > Problem i am facing is,the exported data like Filename,Description,Notes > Others,Reporter Name are exported in separate cell.Will show the output > i am getting and required output. > > You can see my existing and required format. > > I had attached two format,file name existing_csv is the output file i ma > getting,required_csv is the .csv file i am expecting. > > Need some ones help. Peter of the Norse rahmc...@radio1190.org -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Allow users to have a online store on the site
On 20/07/2013 8:36am, Karl Arunachal wrote: Hello, There's one plugin for wordpress called Marketpress E commerce, it has many features, but what i am interested is that, with that plugin users can create online store in your site and sell their products. So, is there any plugin or package like Marketpress for django, which have this features? Check this out ... https://www.djangopackages.com/grids/g/cms/ I think Mezzanine has an online store add-on. I googled in this mater for hours, but didn't got any helpful answers. Please advice if anybody knows about this stuff. Thank you. -- 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. 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. For more options, visit https://groups.google.com/groups/opt_out.
Search from database
Can we have a search module in django that can suggest resembling spellings from mysql database? -- Kamaljeet Kaur Blog:http://kamalkaur188.wordpress.com/ -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: label_tag() escapes output twice
On Friday, 19 July 2013 11:31:27 UTC+2, Tom Evans wrote: > > On Thu, Jul 18, 2013 at 10:31 PM, sephii> wrote: > > Hello, > > > > I'm trying to output a simple form but the label_tag() method tries to > > escape the field label twice. Here's what I have: > > > > # models.py > > # _ is ugettext_lazy, if it matters > > class MyModel(models.Model): > > name = models.CharField(_('My label')) > > > > # forms.py > > class MyForm(forms.ModelForm): > > class Meta: > > model = MyModel > > > > # views.py > > def my_view(request): > > myform = MyForm() > > assert False, myform['name'].label_tag() > > > > Now let's say "My label" is translated as "My 'label'" (with single > quotes > > around it), here's what I get: > > My #39;label#39; > > > > As you can see the single quotes are escaped twice. I tried to > mark_safe() > > the label in my model but then the string is not i18n'ed anymore. Also I > > found a bugreport in a Django-based project that seems related but I > don't > > really understand the fix: > https://github.com/stephenmcd/mezzanine/pull/682. > > I tried using ugettext instead of ugettext_lazy in my model but then the > > labels are not i18n'ed anymore. The only solution that seems to work is > to > > redefine the field in MyForm and set its label using ugettext instead of > > ugettext_lazy, but that seems really complicated given the simple task > I'm > > trying to achieve (also I don't know in advance which fields could have > > "special" entities like quotes). > > > > If I output the form just using {{ form }}, the label is displayed > > correctly. Also {{ form.name.label }} is displayed correctly. {{ > > form.name.label_tag }} is the only method causing the problem. So what > am I > > doing wrong? I'm using Django 1.5. > > > > Thanks, > > Sylvain > > I guess the label_tag() method returns a string that is not marked > safe. If you have to do {{ form.name.label_tag }}, you can always do > {{ form.name.label_tag|safe }}. label_tag() is new in 1.5, this may be > a bug. > > Cheers > > Tom > Hey Tom, Thanks for your answer. I tried your solution but it doesn't work. I checked and label_tag() returns a safe string, otherwise it would not be able to print the tag (because it would be escaped). The problem is with the string that's included in the label, which seems to be escaped twice. Anyway it looks like a bug so I'll just report it. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
How to Create an Auto Incrementing Integer Field
I have a list of articles and you can control the order of the articles by changing the order field in the model order = models.IntegerField(default=1) I would like it to default to one more than the last article or one more than the largest order (since you can set the order) I can't use AutoField because it is only for primary keys. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Django admin automatically adding slash to a URL field after update
Vicherot, thanks a lot for the response. The Django version which I am experimenting this issue is: >>> django.VERSION (1, 4, 2, 'final', 0) Regards Emilio El jueves, 4 de julio de 2013 13:24:27 UTC+2, vicherot escribió: > > I never have that issue, i use default setting of append_slash and never > have a problem with that. (version 1.3) > > > 2013/7/4 Rafael E. Ferrero> >> What version of Django you use ?? >> >> >> 2013/7/4 ecasbas >> >>> Hi, >>> >>> I have this issue wit the admin interface: >>> >>> I try edit manually a register, I change some values and when I save the >>> data, the URL field, which I have no modifed, >>> become automatically with a slash append at the end. This issue is >>> causing inconsistencies due to I normalize each >>> URL before save to BBDD, but when I modify manually some register, then >>> the problem. >>> >>> I tried the advice from: >>> https://docs.djangoproject.com/en/dev/ref/middleware/ >>> APPEND_SLASH = False >>> >>> but without success, any hints? >>> >>> TIA >>> Emilio >>> >>> -- >>> 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...@googlegroups.com . >>> To post to this group, send email to django...@googlegroups.com >>> . >>> Visit this group at http://groups.google.com/group/django-users. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> >> >> -- >> Rafael E. Ferrero >> Claro: (03562) 15514856 >> > > > > -- > Rafael E. Ferrero > Claro: (03562) 15514856 > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Allow users to have a online store on the site
Hello, There's one plugin for wordpress called Marketpress E commerce, it has many features, but what i am interested is that, with that plugin users can create online store in your site and sell their products. So, is there any plugin or package like Marketpress for django, which have this features? I googled in this mater for hours, but didn't got any helpful answers. Please advice if anybody knows about this stuff. Thank you. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Best practice - using Django model's in-built id field?
Hi, Funny you guys should mention that =), after Mike's post, I ended up just using David Cramer's django-uuidfield (https://github.com/dcramer/django-uuidfield) package. (There's also django-shortuuidfield - https://github.com/nebstrebor/django-shortuuidfield). For Postgres, this uses the uuid type - http://www.postgresql.org/docs/9.1/static/datatype-uuid.html). This also sets unique=True - not sure what happens if there's a collision, I assume it just won't create that one. The only thing I was concerned about was a performance hit from calculating the UUID for each transaction, however, I suspect that's something I don't need to worry about until down the track. Cheers, Victor On Friday, 19 July 2013 23:45:06 UTC+10, Javier Guerra wrote: > > On Fri, Jul 19, 2013 at 4:26 AM, Tom Evans >> wrote: > > Because of this, I usually add a uuid field as a unique key, but leave > > id as the primary key. > > same here. > > but only for those tables whose records would be seen by the public. > kinda like 'slugs for non-textual objects' > > -- > Javier > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: Design for storing likes/dislikes?
Hi, Hmm, I'm thinking I might need to go for an enum/choices rather than a NullBooleanField. There are actually four states (so far) that can exist in the relationship between a user and a widget. 1. Like 2. Unlike 3. No vote (i.e. abstain). 4. Not asked yet I suppose I could still use NullBooleanField, but use Like=True, Unlike=False, No Vote = Null, and have that row not exist in the M2M if we haven't asked about that widget yet - however, not sure if that's a bit ambiguous? And it may close the door down the track to other states? Also, it definitely is a M2M between Users and Widgets - since each user can like/unlike many widgets, and each widget can be liked/unliked by many users. Cheers, Victor On Saturday, 20 July 2013 00:57:54 UTC+10, Steven Smith wrote: > > I've used NullBooleanField for this before, as well. A lot quicker to > query on than a ManyToMany, also. > > > > On Tuesday, July 16, 2013 9:24:54 PM UTC-4, donarb wrote: >> >> On Tuesday, July 16, 2013 5:29:47 PM UTC-7, Victor Hooi wrote: >>> >>> Hi, >>> >>> We have a list of users, who are going to like/dislike various widgets. >>> >>> My question is regarding how to store the like/dislikes. >>> >>> Essentially, there can be three states between a user and a widget - >>> like, dislike, and unrated, so it's not just a straight Boolean. >>> >>> I'm thinking of just doing a M2M between user and widgets models, and >>> then storing an extra field on that M2M. >>> >>> I can either store a Boolean, for whether the item is liked/unliked - >>> and then an unrated item simply won't be exist in the table. >>> >>> Or I can store a integer (with a choice tupled defined), with say >>> 0=unrated, 1=like, 2 = unlike, and all possible combinations are listed. >>> >>> We'll be doing queries to compare users, and see if their likes/dislikes >>> intersect. >>> >>> Are there any pros/cons of the two approaches for this given query, or >>> is there perhaps a more efficient way of storing this data? >>> >>> Cheers, >>> Victor >>> >> >> >> >> Django has a NullBooleanField that allows 3 states, Unknown (null), True, >> False. >> >> >> > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: label_tag() escapes output twice
On Fri, Jul 19, 2013 at 4:09 PM, sephiiwrote: > Anyway it looks like a bug so I'll just report it. Can you test things easily with the in-development 1.6 and trunk? There have been a few changes to that code lately and the issue could have been fixed. It's the kind of change that isn't backported to a stable branch like 1.5.x Thanks. -- Ramiro Morales @ramiromorales -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Is Celery the best option?
I had never heard about Rq before, seems interesting indeed! Thanks for the recommendation. Cheers, AT On Fri, Jul 19, 2013 at 4:04 PM, Doug Ballancewrote: > Celery is a good option, and probably the most used. There are a couple > of other options that may be worth looking into: > > Huey > https://github.com/coleifer/huey > > Rq > http://python-rq.org/ > > Personally I've never gotten on well with celery. It's just not at all > intuitive to me (it's heavy use of decorators). Rq was a better fit. > > -- > 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Is Celery the best option?
Celery is a good option, and probably the most used. There are a couple of other options that may be worth looking into: Huey https://github.com/coleifer/huey Rq http://python-rq.org/ Personally I've never gotten on well with celery. It's just not at all intuitive to me (it's heavy use of decorators). Rq was a better fit. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: iregex and word boundaries
I think that you either need to use raw strings (r'stuff') or backslash the backslashes. On Fri, Jul 19, 2013 at 1:34 PM, Michael Soulierwrote: > Hi, > > I'm trying to match a number using word boundaries like so > > record = SipClient.objects.get( > dnlist__iregex='\b%s\b' % dn > ) > > but this is failing to find simple dnlist matches like 1234, while this > does work fine in a python shell. > > >>> SipClient.objects.get(dnlist__iregex='71065') > > >>> SipClient.objects.get(dnlist__iregex='\b71065\b') > Traceback (most recent call last): > File "", line 1, in ? > File "/usr/lib/python2.4/site-packages/django/db/models/manager.py", > line 132, in get > return self.get_query_set().get(*args, **kwargs) > File "/usr/lib/python2.4/site-packages/django/db/models/query.py", > line 348, in get > raise self.model.DoesNotExist("%s matching query does not exist." > DoesNotExist: SipClient matching query does not exist. > > and yet > > >>> import re > >>> re.match(r'\b71065\b', '71065') > <_sre.SRE_Match object at 0xb7f01f38> > > I'm using postgresql for my db. > > Any ideas why this would not work? > > Thanks, > Mike > > -- > 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
iregex and word boundaries
Hi, I'm trying to match a number using word boundaries like so record = SipClient.objects.get( dnlist__iregex='\b%s\b' % dn ) but this is failing to find simple dnlist matches like 1234, while this does work fine in a python shell. >>> SipClient.objects.get(dnlist__iregex='71065') >>> SipClient.objects.get(dnlist__iregex='\b71065\b') Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/site-packages/django/db/models/manager.py", line 132, in get return self.get_query_set().get(*args, **kwargs) File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 348, in get raise self.model.DoesNotExist("%s matching query does not exist." DoesNotExist: SipClient matching query does not exist. and yet >>> import re >>> re.match(r'\b71065\b', '71065') <_sre.SRE_Match object at 0xb7f01f38> I'm using postgresql for my db. Any ideas why this would not work? Thanks, Mike -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Aggregating annotations
Also interestingly, if I do: Book.objects.values('rating').aggregate(Max('rating')) It returns an empty dict without performing any DB query!! On Friday, July 19, 2013 11:49:52 AM UTC+5:30, Debanshu Kundu wrote: > > If I have a model *Book* defined as: > > class Book(models.Model): >name = models.CharField(max_length=300) >pages = models.IntegerField() >price = models.DecimalField(max_digits=10, decimal_places=2) >rating = models.FloatField() >pubdate = models.DateField() > > and I run the query: > > Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max(books_per_rating)) > > I get a *DatabaseError*. > > According to this > https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations, > > Django supports aggregating annotations. But in the example given in the > link itself, they annotate over a *QuerySet* which in turn returns a * > QuerySet* (and not *ValuesQuerySet*), so aggregate method runs > successfully. But in my example aggregating a *ValuesQuerySet* raises a * > DatabaseError*. > > Is it a bug in Django? Because if Django does not support aggregation over > *ValuesQuerySet* then it should raise an exception at Django level (not * > DatabaseError*). > > It returns an empty dict without performing any DB query!! On Friday, July 19, 2013 11:49:52 AM UTC+5:30, Debanshu Kundu wrote: > > If I have a model *Book* defined as: > > class Book(models.Model): >name = models.CharField(max_length=300) >pages = models.IntegerField() >price = models.DecimalField(max_digits=10, decimal_places=2) >rating = models.FloatField() >pubdate = models.DateField() > > and I run the query: > > Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max(books_per_rating)) > > I get a *DatabaseError*. > > According to this > https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations, > > Django supports aggregating annotations. But in the example given in the > link itself, they annotate over a *QuerySet* which in turn returns a * > QuerySet* (and not *ValuesQuerySet*), so aggregate method runs > successfully. But in my example aggregating a *ValuesQuerySet* raises a * > DatabaseError*. > > Is it a bug in Django? Because if Django does not support aggregation over > *ValuesQuerySet* then it should raise an exception at Django level (not * > DatabaseError*). > > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: is Django useful for a basic site as well?
thanks for the tip about this, it looks really useful. I was also pleased to find that, at least in looking at one of the template files, it seems to work well with my screen reader. Too often web developers don't bother with accessibility, but this looks promising. On Jul 18, 2013, at 6:31 PM, Arnold Krillewrote: > On Thu, 18 Jul 2013 17:45:32 -0400 Bill Freeman > wrote: >> Good programmers steal. Great programmers steal from the best. Find >> a beautiful site and don't deviate much from his layout/CSS scheme. > > And some call their work a framework and make everyone use/steal it. > > @Alex: Start with bootstrap (http://twitter.github.io/bootstrap/) and > be blown away by the ease and possibilities. And choose mezzanine for > your django-based cms. > > Of course you still have to do the laymans work of setting up > webservers and databases but there are many tools to help with that > like fabric, chef, puppet... > > Have fun, > > Arnold Have a great day, Alex (msg sent from Mac Mini) mehg...@gmail.com -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Design for storing likes/dislikes?
I've used NullBooleanField for this before, as well. A lot quicker to query on than a ManyToMany, also. On Tuesday, July 16, 2013 9:24:54 PM UTC-4, donarb wrote: > > On Tuesday, July 16, 2013 5:29:47 PM UTC-7, Victor Hooi wrote: >> >> Hi, >> >> We have a list of users, who are going to like/dislike various widgets. >> >> My question is regarding how to store the like/dislikes. >> >> Essentially, there can be three states between a user and a widget - >> like, dislike, and unrated, so it's not just a straight Boolean. >> >> I'm thinking of just doing a M2M between user and widgets models, and >> then storing an extra field on that M2M. >> >> I can either store a Boolean, for whether the item is liked/unliked - and >> then an unrated item simply won't be exist in the table. >> >> Or I can store a integer (with a choice tupled defined), with say >> 0=unrated, 1=like, 2 = unlike, and all possible combinations are listed. >> >> We'll be doing queries to compare users, and see if their likes/dislikes >> intersect. >> >> Are there any pros/cons of the two approaches for this given query, or is >> there perhaps a more efficient way of storing this data? >> >> Cheers, >> Victor >> > > > > Django has a NullBooleanField that allows 3 states, Unknown (null), True, > False. > > > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Aggregating annotations
EDIT: the query should be: Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max('books_per_rating')) *books_per_rating* should be quoted in *Max* call. On Friday, July 19, 2013 11:49:52 AM UTC+5:30, Debanshu Kundu wrote: > > If I have a model *Book* defined as: > > class Book(models.Model): >name = models.CharField(max_length=300) >pages = models.IntegerField() >price = models.DecimalField(max_digits=10, decimal_places=2) >rating = models.FloatField() >pubdate = models.DateField() > > and I run the query: > > Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max(books_per_rating)) > > I get a *DatabaseError*. > > According to this > https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations, > > Django supports aggregating annotations. But in the example given in the > link itself, they annotate over a *QuerySet* which in turn returns a * > QuerySet* (and not *ValuesQuerySet*), so aggregate method runs > successfully. But in my example aggregating a *ValuesQuerySet* raises a * > DatabaseError*. > > Is it a bug in Django? Because if Django does not support aggregation over > *ValuesQuerySet* then it should raise an exception at Django level (not * > DatabaseError*). > > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: field-null or not-null filter?
*You can define your own NullListFilter. I did it this way:* class NullListFilter(FieldListFilter): def __init__(self, field, request, params, model, model_admin, field_path): self.lookup_kwarg = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) super(NullListFilter, self).__init__(field, request, params, model, model_admin, field_path) def expected_parameters(self): return [self.lookup_kwarg] def choices(self, cl): for lookup, title in ( (None, _('All')), ('False', _('Yes')), ('True', _('No'))): yield { 'selected': self.lookup_val == lookup, 'query_string': cl.get_query_string({ self.lookup_kwarg: lookup, }), 'display': title, } FieldListFilter.register(lambda f: True, NullListFilter) *Then you can use it in admin like this:* list_filter = [('field_which_is_foreign_key', NullListFilter)] -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Best practice - using Django model's in-built id field?
On Fri, Jul 19, 2013 at 4:26 AM, Tom Evanswrote: > Because of this, I usually add a uuid field as a unique key, but leave > id as the primary key. same here. but only for those tables whose records would be seen by the public. kinda like 'slugs for non-textual objects' -- Javier -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: Groups Model Randomly Disappears In the Admin View
Thanks for your quick reply:-) I just restarted my Apache server, and so far it seems the quirk has gone. On 19 July 2013 20:04, 刘是wrote: > Thanks for your quick reply:-) I just restarted my Apache server, and so > far it seems the quirk has gone. > > > On 19 July 2013 19:39, Laurence Field wrote: > >> Yes, are using the django shibboleth package? >> >> Laurence >> >> >> >> On 07/19/2013 01:25 PM, 刘是 wrote: >> >> Hi. Did you solve this problem? I'm facing the same problem now. >> >> 在 2011年8月30日星期二UTC+8下午10时44分46秒,Laurence写道: >>> >>> I have a very strange problem that I am having difficultly in >>> debugging. In my application I have registered two models, each of >>> which contains two classes. When I go to the admin view, the groups >>> class from the auth model sometimes disappears. If I keep refreshing, >>> it will appear then disappear almost randomly. Does anyone know what >>> the cause might be or how I can debug this. I am using Django v1.2.5. >>> >>> Thanks, >>> >>> Laurence >>> >> >> > > > -- > 刘是 > -- 刘是 -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: is Django useful for a basic site as well?
On Thu, 18 Jul 2013 17:45:32 -0400 Bill Freemanwrote: > Good programmers steal. Great programmers steal from the best. Find > a beautiful site and don't deviate much from his layout/CSS scheme. And some call their work a framework and make everyone use/steal it. @Alex: Start with bootstrap (http://twitter.github.io/bootstrap/) and be blown away by the ease and possibilities. And choose mezzanine for your django-based cms. Of course you still have to do the laymans work of setting up webservers and databases but there are many tools to help with that like fabric, chef, puppet... Have fun, Arnold signature.asc Description: PGP signature
Re: Groups Model Randomly Disappears In the Admin View
Hi. Did you solve this problem? I'm facing the same problem now. 在 2011年8月30日星期二UTC+8下午10时44分46秒,Laurence写道: > > I have a very strange problem that I am having difficultly in > debugging. In my application I have registered two models, each of > which contains two classes. When I go to the admin view, the groups > class from the auth model sometimes disappears. If I keep refreshing, > it will appear then disappear almost randomly. Does anyone know what > the cause might be or how I can debug this. I am using Django v1.2.5. > > Thanks, > > Laurence > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Aggregating annotations
If I have a model *Book* defined as: class Book(models.Model): name = models.CharField(max_length=300) pages = models.IntegerField() price = models.DecimalField(max_digits=10, decimal_places=2) rating = models.FloatField() pubdate = models.DateField() and I run the query: Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max(books_per_rating)) I get a *DatabaseError*. According to this https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations, Django supports aggregating annotations. But in the example given in the link itself, they annotate over a *QuerySet* which in turn returns a * QuerySet* (and not *ValuesQuerySet*), so aggregate method runs successfully. But in my example aggregating a *ValuesQuerySet* raises a * DatabaseError*. Is it a bug in Django? Because if Django does not support aggregation over * ValuesQuerySet* then it should raise an exception at Django level (not * DatabaseError*). -- 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. For more options, visit https://groups.google.com/groups/opt_out.
request.user returning AnonymousUser
Hello, I have a custom authentication backend and a custom user model. The authentication() method in the custom authentication backend returns the custom user after successful authentication. Once authenticated, I use login from django.contrib.auth to set the sessionid for the custom user. Now, I create a method as shown below: * def myview(request): if not request.user.is_authenticated(): return render(request,'auth_fail.html') else: return render(request,'auth_success.html')* This is always returning not authenticated. When I print request.user, it shows AnonymousUser. But, when i print request.COOKIES, i can see the sessionid which was set by the login method from django.contrib.auth. So, my question is how can I check if the custom user is authenticated or not?. Thanks in advance. Best Regards, Goose. -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Custom authentication backend
Found an answer here: http://stackoverflow.com/questions/10874675/why-does-django-need-a-database-for-custom-authentication-backends On Thursday, July 18, 2013 1:12:04 PM UTC+2, Rok Jaklič wrote: > > Hi, > > if I write custom authentication backend, do I need to create user in > local database so that method get_user can find it? > > Can I avoid creating user in local database? > > Rok > -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: label_tag() escapes output twice
On Thu, Jul 18, 2013 at 10:31 PM, sephiiwrote: > Hello, > > I'm trying to output a simple form but the label_tag() method tries to > escape the field label twice. Here's what I have: > > # models.py > # _ is ugettext_lazy, if it matters > class MyModel(models.Model): > name = models.CharField(_('My label')) > > # forms.py > class MyForm(forms.ModelForm): > class Meta: > model = MyModel > > # views.py > def my_view(request): > myform = MyForm() > assert False, myform['name'].label_tag() > > Now let's say "My label" is translated as "My 'label'" (with single quotes > around it), here's what I get: > My #39;label#39; > > As you can see the single quotes are escaped twice. I tried to mark_safe() > the label in my model but then the string is not i18n'ed anymore. Also I > found a bugreport in a Django-based project that seems related but I don't > really understand the fix: https://github.com/stephenmcd/mezzanine/pull/682. > I tried using ugettext instead of ugettext_lazy in my model but then the > labels are not i18n'ed anymore. The only solution that seems to work is to > redefine the field in MyForm and set its label using ugettext instead of > ugettext_lazy, but that seems really complicated given the simple task I'm > trying to achieve (also I don't know in advance which fields could have > "special" entities like quotes). > > If I output the form just using {{ form }}, the label is displayed > correctly. Also {{ form.name.label }} is displayed correctly. {{ > form.name.label_tag }} is the only method causing the problem. So what am I > doing wrong? I'm using Django 1.5. > > Thanks, > Sylvain I guess the label_tag() method returns a string that is not marked safe. If you have to do {{ form.name.label_tag }}, you can always do {{ form.name.label_tag|safe }}. label_tag() is new in 1.5, this may be a bug. Cheers Tom -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: Best practice - using Django model's in-built id field?
On Fri, Jul 19, 2013 at 2:31 AM, Victor Hooiwrote: > Hi, > > I'm just wondering - is it considered good or bad practice to use a Django > model's in-built ID field? > > Say for example you wanted a unique identifier for each transactio - should > you be generating your own, or can you use just self.id? > The problem with using an auto increment field for a unique identifier is that the values are easily guessable. You are better off adding a uuid field and either using it as the primary key, or adding it as a unique key and keeping id as the pk. The other question is how to store the uuid, which is a 128 bit number. Most databases don't support such large numbers, and so Django doesn't provide a UUIDField. You can always store the uuid in 36 (iirc) text characters, but that makes lookups slightly less efficient. If you use the uuid as the primary key, and you are using a text field to store it in, then any object that has a foreign key to this object will also need a 36 character uuid column - ie there is a cost to having it as the primary key. Because of this, I usually add a uuid field as a unique key, but leave id as the primary key. Cheers Tom -- 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. For more options, visit https://groups.google.com/groups/opt_out.
Re: is Django useful for a basic site as well?
On Thu, Jul 18, 2013 at 10:45 PM, Bill Freemanwrote: > Good programmers steal. Great programmers steal from the best. Find a > beautiful site and don't deviate much from his layout/CSS scheme. There are plenty of free CSS layout reservoirs out there for people to use, advising people that it is fine to rip off other people's designs is not cool. You should only re-use someone else's layout or CSS if they say it is fine to do so. Tom -- 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. For more options, visit https://groups.google.com/groups/opt_out.
bug in multi-level multi-table inheritance?
All, I'm seeing some unexpected behavior when using multi-level multi-table inheritance. In the example below there are three levels in the class hierarchy. When accessing the name field (which is stored in the Level1 base class) from an object in Level3, the query does not seem to walk the hierarchy correctly. Note: I am using explicitly defined id and OneToOneFields so that I can specify the db_column names (for compatibility with other legacy software using the same database). Here's the models file, followed by a console log where I created test objects (in an otherwise blank database): # models.py from django.db import models class Level1(models.Model): """ Top level base class """ level1_id = models.AutoField(primary_key=True, db_column='Level1_ID') name = models.CharField(max_length=32, db_column='Name', blank=True, null=True) class Meta: db_table = 'Level1' class Level2(Level1): """ Middle level class """ level2_id = models.AutoField(primary_key=True, db_column='Level2_ID') level1 = models.OneToOneField('Level1', db_column='Level1_ID', parent_link=True) # parent class class Meta: db_table = 'Level2' class Level3(Level2): """ Bottom level class """ level3_id = models.AutoField(primary_key=True, db_column='Level3_ID') level2 = models.OneToOneField('Level2', db_column='Level2_ID', parent_link=True) # parent class class Meta: db_table = 'Level3' >From the manage.py shell I add two objects to an otherwise empty database: (InteractiveConsole) from sc_test.models import * # add Level1 object top1 = Level1() top1.name = 'Top 1' top1.save() # add Level3 object bot1 = Level3() bot1.name = 'Bot 1' bot1.save() # query for Level1 objects l1 = Level1.objects.all() l1 [, ] # query for Level2 objects l2 = Level2.objects.all() l2 [] # query for Level3 objects l3 = Level3.objects.all() l3 [] # Get name of first Level1 object (looks ok) l1[0].name u'Top 1' # Get name of second Level1 object (looks ok) l1[1].name u'Bot 1' # First Level2 object (looks ok) l2[0].name u'Bot 1' # First Level3 object (!! WRONG !!) l3[0].name u'Top 1' (WRONG - expected this to return u'Bot 1') Can anyone shed some light? Specifically, does multi-table inheritance only work with one level of subclassing? If not, is there some syntax trick I'm missing, or is this a bug in the inheritance code? Thanks, Dow -- 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. For more options, visit https://groups.google.com/groups/opt_out.