Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!

ATLbeer posted:

Something isn't right. Can you post one of your slow views? Your using the development server currently? MySQL is installed locally? What OS?

Yea, using ./manage.py runserver with python 2.5.2, mysql is local, os is Arch Linux.

It doesn't appear that there is any one thing that is dramatically increasing the page load time. There's quite alot of custom queryset code being used, and some model inheritance. I've pasted some of the things that are being called in the view here:

http://pastebin.com/m653d303f

(Tried to use privatepaste but I think I found some kind of security flaw).

Mashi fucked around with this message at 17:06 on Mar 24, 2009

Adbot
ADBOT LOVES YOU

bitprophet
Jul 22, 2004
Taco Defender

Mashi posted:

Hmm.. I'd LOVE to be able to do this. In fact, it seems to make perfect sense to be able to make read only models that could use more complex queries in the backend while being more lightweight in Python, since alot of what I'm doing is trying to squeeze out flexibility from the Django ORM, which is going to load alot of objects for each model that I don't need if I'm only displaying data.

Well, as king_kilr said, check out the new 'managed' option for models:

http://docs.djangoproject.com/en/dev/ref/models/options/#managed

It'll let you ensure that Django won't even try to update that particular DB table, meaning you can actually use SQL views and not worry about errors (which, prior to now, would crop up when you do e.g. manage.py syncdb -- it'd try to create or modify your SQL view, which of course would raise a database error).

The feature is in trunk and I believe it'll be part of the 1.1 release; king_kilr pays closer attention than I do and can confirm/deny that.

mwarkentin
Oct 26, 2004
Yep, that's in the 1.1 beta now: http://docs.djangoproject.com/en/dev/releases/1.1-beta-1/

king_kilr
May 25, 2007

bitprophet posted:

Well, as king_kilr said, check out the new 'managed' option for models:

http://docs.djangoproject.com/en/dev/ref/models/options/#managed

It'll let you ensure that Django won't even try to update that particular DB table, meaning you can actually use SQL views and not worry about errors (which, prior to now, would crop up when you do e.g. manage.py syncdb -- it'd try to create or modify your SQL view, which of course would raise a database error).

The feature is in trunk and I believe it'll be part of the 1.1 release; king_kilr pays closer attention than I do and can confirm/deny that.

Indeed, anything in trunk makes it's way into the release. The Django policy isn't to branch for a release after feature freeze, it's to wait until the the release is done before committing any new features.

Ansible
Sep 11, 2003

This is what I get for trusting other people to give me stuff. And that's why I'm so lucky.
Searched through documentation and couldn't figure out how to do this. Here are the (pared down) tables that I'm using:



I have a ModelForm based on the Consumption model, which works for users to add foods to their log. (I'm assigning User in views.py after they POST the form with User excluded, is there a better way to do this?)

Then I want to display a food log summary with nutrient content for each food. I'm passing my_consumption = Consumption.objects.filter(user_id=request.user) into my template context, then looping through the objects within that and can pull everything I need from the related tables User, Measure, Food.

I run into a problem with the Nutrient table. I have a whole gaggle of Nutrients, but only want to show a handful. The only way that I've found I can access the table is with a loop through consumption_item.measure.food.nutrient_set.all - but I can't filter that to only include Nutr_Def_id = 1 (for example). I want to do something like {{ consumption_item.measure.food.nutrient_set.(filter(id_Nutr_Def=1)) }}.

Should I be passing another context to pull this information, or should I have my model set up differently?

king_kilr
May 25, 2007
Write a template filter/tag to let you do simple operations on a queryset in the template will be the easiest way.

Wulfeh
Dec 1, 2005

The mmo worth playing: DAoC

Ansible posted:

I have a ModelForm based on the Consumption model, which works for users to add foods to their log. (I'm assigning User in views.py after they POST the form with User excluded, is there a better way to do this?)

Doing it like that is fine, there are a variety of ways to accomplish this. Another option would be to do this is by passing the user into the form, and overriding save to add the user in. Like so,

code:
class BlankForm(forms.ModelForm):
        def __init__(self,user,*args,**kwargs):
                self._user = user
                super(forms.ModelForm,self).__init__(*args,**kwargs)
                
        def save(self,*args,**kwargs):
                record = super(BlankForm,self).save(commit=False)
                record.user = self._user
                record.save(*args,**kwargs)
		
		return record                                                                                                           

Ansible posted:

I run into a problem with the Nutrient table. I have a whole gaggle of Nutrients, but only want to show a handful. The only way that I've found I can access the table is with a loop through consumption_item.measure.food.nutrient_set.all - but I can't filter that to only include Nutr_Def_id = 1 (for example). I want to do something like {{ consumption_item.measure.food.nutrient_set.(filter(id_Nutr_Def=1)) }}.

Should I be passing another context to pull this information, or should I have my model set up differently?

I wouldn't do this in the template, filter the nutrients you want from the view, and pass it from the view to the template.

MetaKnyght
May 18, 2004
My name is Jeff, not to be confused with the legendary eBay meta-scammer MyNameIsJeff

Ansible posted:

Here are the (pared down) tables that I'm using:

This may be a stupid question, but what software did you use to make that diagram? My Google queries are proving useless.

Ansible
Sep 11, 2003

This is what I get for trusting other people to give me stuff. And that's why I'm so lucky.

MetaKnyght posted:

This may be a stupid question, but what software did you use to make that diagram? My Google queries are proving useless.

It's a webapp and I freakin love it. Here is a hosted version but it's also very small and you can throw it on your own space pretty easily:

http://ondras.zarovi.cz/sql/demo/

Thanks for the responses, I was hoping for a built in template function but I think that I see the way to do this via views - if not, i'll build out some templating

mwarkentin
Oct 26, 2004
Looks like a nice talk from Jacob Kaplan-Moss will be up from Pycon.

"Real World Django"

Slides are here: http://jacobian.org/speaking/2009/real-world-django/

Video / transcript coming.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...
An odd problem: I'm using Django forms in another project due to two different websites (one Django, one Plone) needing the same forms and behaviour on each. (Long story involves connection to a common resource.) Well and fine, I'm happy with django.forms, I know how to use it, it's simple and well behaved but ...

When you try to get the unicode / string version of an error from a Django form it calls a series of functions that move deep inside Django, look for the internationalization setting and end up trying to load the Django configuration file. Obviously this is no good for my "outside of Django" mode, so I overrode the rendering of the form to try and get the value of the error message directly.

But, the errors are actually proxies that are lazily evaluated for whatever reason. (Translation?) And in some cases proxies containing proxies. These proxy objects are generated by the fields clean function This makes it hard to get at the actual value. Currently I'm calling "e._proxy____args[0]._proxy____args" which feels pecarious. Anyone got any other solutions or ideas?

Things I've thought of:

* Subclassing every field class I use to get it to create actual strings not proxies. Blergh.

* Monkey patching the calling functions on the fields. Blergh.

* Put a fake config file in the calling module and fooling the Django machinery into thinking this is the config file. Blergh.

king_kilr
May 25, 2007
if the issue is not having a django settings file you can always do:

from django.conf import settings
settings.configure(MY_SETTING=value, OTHER_SETTING=value)

To set up the right settings for there, because what you're seeing is django's internationalizating framework, you can probably coerce them to strings with just:

settings.configure(USE_I18N=False)

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

king_kilr posted:

To set up the right settings for there, because what you're seeing is django's internationalizating framework, you can probably coerce them to strings with just:

settings.configure(USE_I18N=False)

Thanks! That's 30 lines shorter than the solution I spent all morning on ...

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!
Back to my performance issue:

code:
for record in SearchRecord.objects.select_related('content_type').all():
    related_object = record.content_object
This will generate n+1 queries. Loading each related object seems to take 10ms, when the query to fetch it takes 0.5ms. Does this seem right? It seems like a hell of alot to me.

Dual core box, local mysql, Django dev server.

EDIT: SOLUTION

The problem turns out to be the Debug Toolbar itself!

code:
With toolbar:

[mashi@leaf ~]$ ab -c 1 -n 10 "http://localhost:8000/sud/busqueda/?q=los"
Requests per second:    0.59 [#/sec] (mean)
Time per request:       1696.515 [ms] (mean)

Without toolbar:

[mashi@leaf ~]$ ab -c 1 -n 10 "http://localhost:8000/sud/busqueda/?q=los"
Requests per second:    7.90 [#/sec] (mean)
Time per request:       126.559 [ms] (mean)

Mashi fucked around with this message at 18:30 on Mar 26, 2009

ATLbeer
Sep 26, 2004
Über nerd
Not quite ready for the OP but, I want to share this with you all first just to get some feedback and some basic error checking. I'm looking to finish off these tutorials sometime this week and start a new thread for 1.0+ since the OP here is quite old.

http://simpledjango.com/tutorial/

geera
May 20, 2003

ATLbeer posted:

Not quite ready for the OP but, I want to share this with you all first just to get some feedback and some basic error checking. I'm looking to finish off these tutorials sometime this week and start a new thread for 1.0+ since the OP here is quite old.

http://simpledjango.com/tutorial/
I've read through the first couple pages and it seems well enough, but there have been several grammar/spelling/punctuation issues. Just figured you'd want to know so you can polish it up a bit.

Otherwise thanks for the tutorial, I can't get enough of them being the Django newbie that I am :)

ATLbeer
Sep 26, 2004
Über nerd

geera posted:

I've read through the first couple pages and it seems well enough, but there have been several grammar/spelling/punctuation issues. Just figured you'd want to know so you can polish it up a bit.

Otherwise thanks for the tutorial, I can't get enough of them being the Django newbie that I am :)

That sounds like my writing :D

Yeah, there will probably wind up being about 12 sections in total. They I'm going to fill in some of the Quick Reference material.

This blog isn't meant to replace the formal documentation but, merely augment. It's just going to show a basic tutorial and some normal design patterns for application development

geera
May 20, 2003

ATLbeer posted:

This blog isn't meant to replace the formal documentation but, merely augment. It's just going to show a basic tutorial and some normal design patterns for application development
I would be interested in seeing your sections on design patterns for Django applications. It seems like every tutorial focuses on creating a certain type of application, usually a blog, but doesn't really give best practices for just using Django to develop a regular web site with content that isn't based around articles or blog posts or whatever.

For example, is it best to create an application just for the site's content? So the index of https://www.mysite.com maps to an app in /mysite/main or /mysite/core, which handles serving the whole site? That seems a bit hamfisted, but I haven't seen that properly explained anywhere yet.

Ansible
Sep 11, 2003

This is what I get for trusting other people to give me stuff. And that's why I'm so lucky.
Anyone here have experience implementing a graphing systems in a Django app? I am curious if there is an existing API that is solid, or if I should try to hook into one of Google's widgets.

To continue my previous example, I record calories on a daily basis. I then want to graph that over time with a trailing average, and be able to select the duration then update through AJAX. Most graphs that I see look good-awful, or are done in flash.

I've also considered throwing this on Google's App Engine, would that make it easier to link to their widget APIs? I absolutely love their graphing tools, even though most are done in flash. I've never worked with piping data to a flash applet though (but i've never played with python either soo)

Sharktopus
Aug 9, 2006

Ansible posted:

Anyone here have experience implementing a graphing systems in a Django app? I am curious if there is an existing API that is solid, or if I should try to hook into one of Google's widgets.

To continue my previous example, I record calories on a daily basis. I then want to graph that over time with a trailing average, and be able to select the duration then update through AJAX. Most graphs that I see look good-awful, or are done in flash.

I've also considered throwing this on Google's App Engine, would that make it easier to link to their widget APIs? I absolutely love their graphing tools, even though most are done in flash. I've never worked with piping data to a flash applet though (but i've never played with python either soo)

I'm currently using the google chart API, but if that isn't pretty enough then I'm sure a python library that uses GD to make pretty charts exists if you do a little googling.

slobodan
Aug 2, 2000

Ansible posted:

Anyone here have experience implementing a graphing systems in a Django app? I am curious if there is an existing API that is solid, or if I should try to hook into one of Google's widgets.

I've been building a JS charting thing using the OpenLayers mapping library, and it has worked out better than I had expected. It is a ridiculous way of doing it though. I was using the jQuery library flot for a while, and it's great. Easy to use, and the API is simple enough.

ATLbeer
Sep 26, 2004
Über nerd

slobodan posted:

I was using the jQuery library flot for a while, and it's great. Easy to use, and the API is simple enough.

Flot's the way to go. I tried using Google's Visualization API's but, honestly it's really a mess. I found their methodology to graph construction to to be a bit cumbersome and not intuitive. Flot rocked. I pointed Flot to a URI that spit out JSON and it made a graph and it rocked

Mulozon Empuri
Jan 23, 2006

geera posted:

I would be interested in seeing your sections on design patterns for Django applications. It seems like every tutorial focuses on creating a certain type of application, usually a blog, but doesn't really give best practices for just using Django to develop a regular web site with content that isn't based around articles or blog posts or whatever.

For example, is it best to create an application just for the site's content? So the index of https://www.mysite.com maps to an app in /mysite/main or /mysite/core, which handles serving the whole site? That seems a bit hamfisted, but I haven't seen that properly explained anywhere yet.

Some tutorials featuring topics such as these would kick rear end. Please do this.

Also, mathplotlib?

Sharktopus
Aug 9, 2006

Just got my current project using flot instead of google chart API and flot looks so much nicer and has all the fancy rollover stuff built in.

Great recommendation.

Ansible
Sep 11, 2003

This is what I get for trusting other people to give me stuff. And that's why I'm so lucky.

Mulozon Empuri posted:

Some tutorials featuring topics such as these would kick rear end. Please do this.

Also, mathplotlib?

I agree. Is there a good source for best practices in django development? My entire site is just one app, but I'm considering splitting it up into functional areas based on related tables/models.


Flot looks pretty cool, I'll play with that over the weekend.

king_kilr
May 25, 2007

Ansible posted:

I agree. Is there a good source for best practices in django development? My entire site is just one app, but I'm considering splitting it up into functional areas based on related tables/models.


Flot looks pretty cool, I'll play with that over the weekend.

Read anything james bennet writes(see his DjangoCon video on youtube on reusable applications) his look, Practical Django Development is also good.

politicorific
Sep 15, 2007
I'm planning on creating a new google appengine project soon that will create web pages with artwork determined by user input (basically bezier curves showing relationships). The best method I've come up with is to use SVGs since I want them to scale correctly. I would use in-line SVG code in xhtml, but in-line svg isn't fully supported by all browsers (safari seems to put 100pixel margins at the bottom of any inline svg code). So instead I'm planning on using object= and embed= code, however this means I must create the files before sending. If I can't access the file system on app engine, how can I do this?

I'm just doing theory right now, so the short version is, how do I dynamically create files to send to users without doing anything to the file system?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

politicorific posted:

I'm planning on creating a new google appengine project soon that will create web pages with artwork determined by user input (basically bezier curves showing relationships). The best method I've come up with is to use SVGs since I want them to scale correctly. I would use in-line SVG code in xhtml, but in-line svg isn't fully supported by all browsers (safari seems to put 100pixel margins at the bottom of any inline svg code). So instead I'm planning on using object= and embed= code, however this means I must create the files before sending. If I can't access the file system on app engine, how can I do this?

I'm just doing theory right now, so the short version is, how do I dynamically create files to send to users without doing anything to the file system?

I don't know exactly how to do this in Django, but the basic idea (I think) is you'd have a view that spews out the SVG, then get Django to write the headers as if you're sending an SVG (because you are). (That's the part I'm not sure about in Django.) Then, in the template where the SVG is to appear, point it to the right URL for the aforementioned view.

bitprophet
Jul 22, 2004
Taco Defender

pokeyman posted:

I don't know exactly how to do this in Django, but the basic idea (I think) is you'd have a view that spews out the SVG, then get Django to write the headers as if you're sending an SVG (because you are). (That's the part I'm not sure about in Django.) Then, in the template where the SVG is to appear, point it to the right URL for the aforementioned view.

This is correct. Headers are stupid easy, just use dictionary syntax on a response object. Here's an off-the-top-of-my-head, these-are-probably-not-the-right-headers example:
code:
def my_svg_view(request):
    svg_data = generate_some_binary_svg_data()
    response = HttpResponse()
    response['Content-Type'] = 'img/svg' # or whatever the mimetype is
    response['Content-Length'] = len(svg_data) # assuming the SVG Python object makes it that easy to get its length
    response['Content-Disposition'] = 'attachment;filename=my_image.svg'
    response.write(svg_data)
    return response
For something intended to be displayed in-browser you'd probably leave off any content-disposition header, but I just threw it in there as it's a common example for downloadable files v:shobon:v

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!
Anyone have a preferred method for storing additional profile data against users? Using a user profile makes the account creation / editing forms a bit messy. Anyone had success subclassing User, or know a nice way to do the forms for a User with a custom profile?

king_kilr
May 25, 2007

Mashi posted:

Anyone have a preferred method for storing additional profile data against users? Using a user profile makes the account creation / editing forms a bit messy. Anyone had success subclassing User, or know a nice way to do the forms for a User with a custom profile?

Just use a user profile model, that's how it's done. Needing to hook up a signal reciever and handle 2 ModelForm's isn't difficult, it's like 10 lines of code.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

Just use a user profile model, that's how it's done. Needing to hook up a signal reciever and handle 2 ModelForm's isn't difficult, it's like 10 lines of code.

Man if only we had an example

ATLbeer
Sep 26, 2004
Über nerd

m0nk3yz posted:

Man if only we had an example

http://sam.bluwiki.com/blog/2008/05/extending-user-model-profiles-in-django.php
code:
from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    karma = models.IntegerField(default=1, core=True)
    url = models.URLField(default="", core=True)
    user = models.ForeignKey(User, unique=True)
settings.py
code:
AUTH_PROFILE_MODULE = 'core.UserProfile'
Signals... Haven't played with them that much yet
http://docs.djangoproject.com/en/dev/topics/signals/

king_kilr
May 25, 2007

m0nk3yz posted:

Man if only we had an example

Yes, if only

Captain Lou
Jun 18, 2004

buenas tardes amigo
Try Pinax, it's got user profile stuff built in and I think makes it easier. It's also great for adding any kind of community functionality to your site.

Yay
Aug 4, 2007
Ok ignore everything below, after spending hours googling, I'm apparently a spaztard, because 'django file uploads' tells me exactly what I needed to know, I needed to pass in request.FILES and POST. you win, Internet.

So I'm trying to get to grips with Django -- I've got a bit of python experience behind me (fiddling with cherrypy, and qt) -- but I'm hitting a weird stumbling block with models and forms.

I've got a model like so:
code:
class Image(models.Model):
    parent = models.ForeignKey(Gallery)
    deleted = models.BooleanField(default=False)
    created = models.DateTimeField('date created', editable=False, default=datetime.now())
    modified = models.DateTimeField('date modified', default=datetime.now())
    img = models.FileField(max_length=100, upload_to='images') # yes, it should be an ImageField, except PIL isn't installed here
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=250, blank=True)
along with a form and admin already setup; both of which have the fields:
code:
fields = ('parent', 'img', 'title', 'description',)
When using the auto admin, I can fill in the form, upload a file and have it save - super, thats what I'm after. When calling it via the front end, the FileField always makes the form invalid (no error nor traceback, just 'this field is required') using {{ form.as_ul }}

The view I'm working with is like so:
code:
    if request.method == 'POST':
        form = UploadImageForm(request.POST)
    else:
        form = UploadImageForm()

    if form.is_valid():
        form.save()
I presume its something to do with filling it from request.POST, but the tutorial doesn't cover uploads afaik, and the documentation doesn't make it clear how to handle things in file/image fields.

Can anyone shed some light on how to handle this? without file fields, this process works fine, hence my suspicisons about request.POST

Yay fucked around with this message at 14:05 on Apr 8, 2009

ATLbeer
Sep 26, 2004
Über nerd

Yay posted:

Ok ignore everything below, after spending hours googling, I'm apparently a spaztard, because 'django file uploads' tells me exactly what I needed to know, I needed to pass in request.FILES and POST. you win, Internet.

Don't worry. I can't tell you the number of times I've reinvented the wheel in Django and in Python. When I have a something I'm looking for a solution now the first thing I do is browse through the Python docs for a few minutes then browse the Django docs. Usually something new will stick out at me and usually lead me to a solution and make me learn a new module or builtin

king_kilr
May 25, 2007
I'll save the next guy some time and put in a reminder to make sure you have enctype set on your <form> tags.

tayl0r
Oct 3, 2002
This post brought to you by SOE
I've been using the admin site and I broke something in one of the models.
Now, when I click on a row to view it in detail, I get this error:

code:
Exception Value:	
Caught an exception while rendering: %d format: a number is required, not Stringtable
Exception Location:	c:\python26\lib\site-packages\django\template\debug.py in render_node, line 81
my URL looks like this:
code:
http://localhost:8000/admin/leaderboardAdmin/codestringmappings/139668/
139668 is the ID of the code string mappings entry I want to view.

Here is the table it is trying to view:
code:
class CodeStringMappings(models.Model):
    string_id = models.ForeignKey(Stringtable, db_column='STRING_ID', primary_key=True, verbose_name="String ID")
    message_name = models.CharField(max_length=4000, db_column='MESSAGE_NAME', unique=True, verbose_name="Map ID")
    description = models.CharField(max_length=4000, db_column='DESCRIPTION', verbose_name="Description")

    class Meta:
        db_table = u'CODE_STRING_MAPPINGS'
        verbose_name = "Code String Map"
        verbose_name_plural = "Code String Mappings"

    def __unicode__(self):
        return "%d: %s" % (self.string_id, self.message_name)

class CodeStringMappingsAdmin(admin.ModelAdmin):
    list_display = ['string_id', 'message_name', 'description']
    list_filter = ['description']
    #fields = ['string_id', 'message_name', 'description']
    #raw_id_fields = ['string_id']
    search_fields = ['^message_name']
That able is also linked to the Stringable:
code:
class Stringtable(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True, verbose_name="ID") # Field name made lowercase.
    text = models.CharField(max_length=4000, db_column='TEXT', blank=False, verbose_name="Text") # Field name made lowercase.

    class Meta:
        db_table = u'STRINGTABLE'
        verbose_name = "String"
        verbose_name_plural = "Strings"

    def __unicode__(self):
        return "%d: %s" % (self.id, self.text)

class StringtableAdmin(admin.ModelAdmin):
    list_display = ['id', 'text']
    search_fields = ['=id', '^text']
The error is fairly descriptive, something is getting a Stringtable object instead of an integer. Unfortunately, I've been looking at the code and looking at the Django admin documetnation and I don't see anything that looks broken.

Anyone see anything?

Adbot
ADBOT LOVES YOU

bitprophet
Jul 22, 2004
Taco Defender
code:
class CodeStringMappings(models.Model):
    string_id = models.ForeignKey(Stringtable, blah blah blah)
Can you guess what the value of <CodeStringMappings instance>.string_id is going to be? Here's a tip: it's not an ID number.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply