django is it faster to go to settings or database - django

I am using the sites framework in django. I have a context processor that gives me access to the sites framework which is stored as a model in the database. I could also store the same values in settings.
Is it more efficient to store and retrieve info like site name from the model or from a property in settings?

When it comes to the sites framework, the sites are cached by default and as fast as your settings.
It would be a good idea to keep your settings module for just settings, not as some sort of ad hoc caching mechanism or a replacement for the sites framework.

The settings are cached by default, the database is not. So the settings are definitely faster.
Both are cacheable however so in the end it doesn't matter too much depending on how you use it. Also, it will probably have a negligible amount of influence.

Related

UI design for Django (making APIs or not)

My UI designer is making me a rich html and java script front-end which is supposed to work with my Django server. I have provided him a bunch of APIs to get different elements from the server. (i.e. an api to load the images, another api to load some text, another api to load bigger size image when somebody clicked on a small one, etc.) The problem with this is that every time a user visits the page, I will be getting some number of requests.
On the other hand, I could have used django template tagging to render the page once with all the elements needed. however,
I was wondering if there is a clear benefit in one of these options?
As #limelights said, try not to optimise too early. Because you don't really know what will be your bottleneck.
And when you'll have problems, the first thing to check is your queries to the database. Do you use selectect_related() and are your indexes optimal for your requests. This include the queries that javascript make via django (Try to load all the child node needed in one or two requests, as select_related() do).
After that the next optimisation should probably be cache. Here's the doc about the django's cache framwork.
After that there's adanced optimisation with database denormalization, apache or nginx optimisation, mysql or postgresql settings optimisation, load balancing and etc... Those optimizationg should be done because you have a problem of popularity, not just for a few users.

white labeling and Django application

I've just been approached by my boss about the possibility of white labeling our Django-based site for a new, large customer. Basically, it sounds like it would mainly be changing the graphics/logos on the site. But, I'm sure that at some point they will want to start tweaking the business logic as well. I'm still unclear whether the desire is for it to be a subdomain (e.g. customer.mydomain.com) or a completely new custom domain. Ultimately, it will need to share the database with the rest of the system (for multiple reasons). Is this a good use of the Sites Framework in Django? Or is there a better way of doing this?
More info: Django 1.3.1; PostgreSQL 9.1; Hosted on Heroku
Use a combination of middleware (to set the active site) and context processors (to act on that). You could try to hook into contrib's site framework, but I've always outgrown it rather quickly.

What tools are there for monitoring the django 'locmem' cache?

I'm am trying to optimise some caching for a complex django project, which is struggling with a large number of sql queries.
I'm using 'johnny-cache' to simplify the caching of the queries. It invalidates cached entries for a 'model' on 'save' of data to the model, and as such is geared to sites which are mainly read-only.
I have seen a 'memcached' django admin app which i have used in the past, though cannot find it atm. I would really like a way of monitoring django's "locmem" cache backend, does anyone have any suggestions?

Django deploying as SaaS (basecamp style)

I am almost done developing a Django project (with a few pluggable apps).
I want to offer this project as a SaaS (something like basecamp).
i.e: project1.mysaas.com , project2.mysaas.com etc
I seek your expertise in showing me the path.
Ways I have thought of are:
1 use Sites to define site specific settings.py
2 a middleware to detect request then set settings accordingly
3 create Django project (taking in pluggable apps) for each site
Thanks.
btw, i am a total newbie.
Your requirements aren't at all clear but I'll assume you aren't doing anything tricky and also assume your "project1", "project2" are customer names which won't need any special branding.
First, about your ideas:
You probably won't need to use the sites framework unless each site is branded differently. The site framework works well doing what it was designed to do, which is present different views of a common set of data.
This would work but probably is not the best approach IMO.
This is unmanageable.
Now, this is a really hard topic because there are so many issues. A decent place to start reading is the High Scalability Blog and especially relevant for you would be the post on 37signals Architecture.
Finally, here's what I am doing in a small SaaS app (that doesn't need extreme scalability):
Use the sites framework (because user pages will be branded by the partner/reseller and each partner has a unique login page)
Use mod_wsgi to minimize resource usage from all the Django instances.
Instead of middleware I put a common code at the top of every view that identifies the company of the user. I need this for logic in the views which is why I don't think it's useful in middleware.

How to configure server for small hosting company for django-powered flash sites?

I'm looking at setting up a small company that hosts flash-based websites for artist portfolios. The customer control panel would be django-powered, and would provide the interface for uploading their images, managing galleries, selling prints, etc.
Seeing as the majority of traffic to the hosted sites would end up at their top level domain, this would result in only static media hits (the HTML page with the embedded flash movie), I could set up lighttpd or nginx to handle those requests, and pass the django stuff back to apache/mod_whatever.
Seems as if I could set this all up on one box, with the django sites framework keeping each site's admin separate.
I'm not much of a server admin. Are there any gotchas I'm not seeing?
Maybe. I don't think the built-in admin interface is really designed to corral admins into their own sites. The sites framework is more suited to publish the same content on multiple sites, not to constrain users to one site or another. You'd be better off writing your own admin interface that enforces those separations.
As far as serving content goes, it seems like you could serve up a common (static) Flash file that uses a dynamic XML file to fill in content. If you use Django to generate the XML, that would give you the dynamic content you need.
This django snippet might be what you need to keep them seperate:
http://www.djangosnippets.org/snippets/1054/
"A very simple multiple user blog model with an admin interface configured to only allow people to edit or delete entries that they have created themselves, unless they are a super user."
Depending on the amount of sites you're going to host it might be easier to write a single Django app once, with admin, and to create a separate Django project for each new site. This is simple, it works for sure AND as an added bonus you can add features to newer sites without running the risk of causing problems in older sites.
Then again, it might be handier to customize the admin such that you limit the amount of objects users can see to those on the given site itself. This is fairly easy to do, allthough you might want to use RequestSite instead of the usual Site from the sites framework as that requires separate settings for each site.
There exists this one method in the ModelAdmin which you can override to have manual control over the objects being edited.