django

Code reuse between django and appengine Model classes

I created a custom django.auth User class which works with Google Appengine, but it involves a fair amount of copied code (practically every method). It isn't possible to create a subclass because appengine and django have different database models with their own metaclass magic. So my question is this: is there an elegant way to copy...

Contextual form validation in Django

I want to do "contextal" form validation in django. Consider this case: PLACE_TYPES = ( ('RESTAURANT', 'Restaurant'), ('BARCLUB', 'Bar / Club'), ('SHOPPING', 'Shopping'), ) RESTURANT_FORMAT_CHOICES = ( ('FAST_FOOD', 'Fast Food'), ('FAST_CASUAL', 'Fast Casual'), ('CASUAL', 'Casual'), ('CHEF_DRIVEN', 'Chef Dr...

Edit .htaccess from within Django admin

I block certain referers in my .htacccess file to avoid serving images to warez sites. The images are served directly so Django doesn't touch them and I'd like to keep it that way because of performance. But I would like to be able to add more blocked sites to the list inside the .htaccess file using the Django admin, without having to ...

Custom Filter in Django Admin

How can I add a custom filter to django admin (the filters that appear on the right side of a model dashboard)? I know its easy to include a filter based on a field of that model, but what about a "calculated" field like this: class NewsItem(models.Model): headline = models.CharField(max_length=4096, blank=False) byline_1 = mod...

django for loop counter break

This is hopefully a quick/easy one. I know a way to work around this via a custom template tag, but I was curious if there were other methods I was over looking. I've created a gallery function of sorts for my blog, and I have a gallery list page that paginates all my galleries. Now, I don't want to show all the photos of each gallery in...

Create zip archive for instant download

In a web app I am working on, the user can create a zip archive of a folder full of files. Here here's the code: files = torrent[0].files zipfile = z.ZipFile(zipname, 'w') output = "" for f in files: zipfile.write(settings.PYRAT_TRANSMISSION_DOWNLOAD_DIR + "/" + f.name, f.name) downloadurl = settings.PYRAT_DOWNLOAD_BASE_URL + "/"...

django db writes not happening immediately when running in admin site

I have a snippet of code like this in a Django application def update_account(user_account,add_widget_count): user_account.add_widgets(add_widget_count) user_account.save() notify_other_system_about_account_update(user_account) When I run this as part of my application, everything works great. In particular, when my other...

Django: Retrieve active threads by the time the last Post was created

Hallo people, I have two Models, Thread and Post, where one Thread can have many Posts. I want to retrieve the active threads by sorting by 'post_set.createtime'. In the end, I want to get exactly ten Threads that had the most recent activity. Is this possible using no own SQL? Thanks a lot in advance. [Copying the model definitions f...

Django admin: turning off DB transactions

I noticed that by default, all updates in the django admin site are done as transactions. I need to either: - turn off transactions (globally or for a particular admin view) - inside of a save() method of an entity being saved via the admin interface, commit the transaction The reason is that I overrode the save() method, and am noti...

How to configure code completion for Django based projects in PyDev?

I am playing with a simple project based on Django framework. My IDE is PyDev/Eclipse. I cannot make code completion work for Django code, but it works fine for standard Python libraries. I tried to add Django dir (in my case C:\Program Files\Python26\Lib\site-packages\django) to PYTHONPATH both on PyDev level (Window->Preferences->PyDe...

Is the Python Imaging Library not available on PyPI, or am I missing something?

easy_install pil results in an error: Searching for pil Reading http://pypi.python.org/simple/pil/ Reading http://www.pythonware.com/products/pil Reading http://effbot.org/zone/pil-changes-115.htm Reading http://effbot.org/downloads/#Imaging No local packages or download links found for pil error: Could not find suitable distribution fo...

How can this be written on a single line?

I've seen some Python list comprehensions before, but can this be done in a single line of Python? errs = {} for f in form: if f.errors: errs[f.auto_id] = f.errors ...

Using data from django queries in the same view

I might have missed somthing while searching through the documentation - I can't seem to find a way to use data from one query to form another query. My query is: sites_list = Site.objects.filter(worker=worker) I'm trying to do something like this: for site in sites_list: [Insert Query Here] Edit: I saw the awnser and im not s...

Internationalization of python/django program: how to implement placeholder in .po file?

E.g. in my django source I have: {% blocktrans %}You are a member since {{sincewhen}}{% endblocktrans %} What should go into the .po file? (django docs don't explain this part as far as I could find) Is .po format for placeholders universal for use in programs written in different programming languages? edit: I've tried commands dj...

Django ImportError at / no matter what I do

So I've just started playing around with Django and I decided to give it a try on my server. So I installed Django and created a new project, following the basics outlined in the tutorial on Djangoproject.com Unfortunatly, no matter what I do, I can't get views to work: I constantly get ImportError at / No module named index Here i...

Django: Displaying only the text of a form in admin

Ok for something that sounds easy, im having some problems looking for an answer. I have this model: class Page(models.Model): creator = models.TextField(blank=False) name = models.CharField(max_length=70, primary_key=True) name_slug = models.SlugField(max_length=70, unique=True) whenever I save in admin i...

Object Ownership in Django

I'm wondering how I might accomplish a simple 'object ownership' system with django models, such that, by default, only the owner of an object may edit it. I am attempting to allow a 'Management' group to edit all objects on behalf of the object owners, and have at this point added a custom permission: class Meta: permissions =...

Generic Views from the object_id or the parent object

I have a model that represents a position at a company: class Position(models.Model): preferred_q = ForeignKey("Qualifications", blank=True, null=True, related_name="pref") base_q = ForeignKey("Qualifications", blank=True, null=True, related_name="base") #[...] It has two "inner objects", which represent minimum qualifica...

Writing tests for Django's admin actions

I'm using Django 1.1 beta and hoping to use admin actions. I have to write unit tests for those, but I don't get it how to write tests for them. For normal view handler functions, I can use Django's TestClient to simulate http request/response, but how should it be done with admin actions? ...

xml.dom.minidom Document() in Python/django outputting memory location

I'm learning Python and django at the same time. I'm trying to create an xml document to return some XML from a view. I'm using the django development server at the moment and I keep getting this information spitting out in my views instead of the document I tried to create. Here's my code from django.http import HttpResponse from ...