django

Use the Django ORM in a standalone script (again)

I'm trying to use the Django ORM in some standalone screen scraping scripts. I know this question has been asked before, but I'm unable to figure out a good solution for my particular problem. I have a Django project with defined models. What I would like to do is use these models and the ORM in my scraping script. My directory structur...

Django migrations--is it possible to use South in the middle of the project?

I already started a project, and the models are all synced and everything. ...

In Django, what is a one-to-one relationship?

I've always been using ForeignKeys. ...

In Django, why is user.is_authenticated a method and not a member variable like is_staff

Hello all, I've lost some time with a bug in my app due to user authentication. I think that it's a bit confusing but maybe someone can explain the reason and it will appear to me very logical. The user.is_staff is a member variable while user.is_authenticated is a method. However is_authenticated only returns True or False depending i...

Get entry by week number using Django ORM?

I tried something like: MyModel.objects.filter(year__week=1) It doesn't work. For now, I calculate the first day and the last day of the week and then use gte and lte, but it's less than efficient given that SQL comes with a Week function. ...

django ManyToMany through help

Hay I've got a question about relationships. I want to Users to have Friendships. So a User can be a friend with another User. I'm assuming i'll need to use the ManyToManyField, through a Friendship table. But i cannot get it to work. Any ideas? Here are my models. class User(models.Model): username = models.CharField(max_length=9...

How would I write this query in GeoDjango? (It's a library for geographic calculations in Django)

Right now I'm using raw SQL to find people within 500 meters of the current user. cursor.execute("SELECT user_id FROM myapp_location WHERE\ GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s"\ ,(user_utm_easting, user_utm_northing, 500)); How would I do this in GeoDjango? It...

How to get a template tag to auto-check a checkbox in Django

I'm using a ModelForm class to generate a bunch of checkboxes for a ManyToManyField but I've run into one problem: while the default behaviour automatically checks the appropriate boxes (when I'm editing an object), I can't figure out how to get that information in my own custom templatetag. Here's what I've got in my model: from mypro...

How to customize pickle for django model objects

My app uses a "per-user session" to allow multiple sessions from the same user to share state. It operates very similarly to the django session by pickling objects. I need to pickle a complex object that refers to django model objects. The standard pickling process stores a denormalized object in the pickle. So if the object changes ...

Django: Adding inline formset rows without javascript

This post relates to this: http://stackoverflow.com/questions/520421/add-row-to-inlines-dynamically-in-django-admin Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there would be a page-refresh involved. So, if the form had a button called 'add'... I figured I could do it like this: if request.met...

How to limit columns returned by Django query ?

That seems simple enough, but all Django Queries seems to be 'SELECT *' How do I build a query returning only a subset of fields ? ...

Python Image Library, Close method

Hello, I have been using pil for the first time today. And I wanted to resize an image assuming it was larger than 800x600 and also create a thumbnail. I could do either of these tasks separately but not together in one method (I am doing a custom save method in django admin). This returns a "cannot identify image file" error message. T...

What's best practice to check if an object is part of a ManyToMany relationship in Django

from an instance of Site with a ManyToMany relationship to Kiosk, i'd like to check if a Kiosk object is part of the relationship. I could do self.apps.get(id=app_id).exists() and check if True or self.apps.get(id=app_id) and catch the ObjectDoesNotExist error or self.apps.filter(id=app_id) and check if True If I have to ca...

Django save_m2m() and excluded field

UPDATE: I ended up creating a new forms.Form instead of using a ModelForm hi, in a ModelForm I replaced a field by excluding it and adding a new one with the same name, as shown below in AddRestaurantForm. When saving the form with the code shown below, I get an error in form.save_m2m() ("Truncated incorrect DOUBLE value"), which seems...

Django: do I need to do HttpResponseRedirect to render a simple string after a POST?

I've got a mobile app that makes POST requests to a Django site. I want to return a simple string (not a template-based page) after the app makes the POST request, saying 'Success' or 'Failure' (EDIT: plus a bit of extra info) as appropriate. **EDIT: note that it's not a browser making the request, it's an app (so there's no BACK butt...

Will Django user permissions work for models with inline tabular forms of other models?

I am setting up DJango admin to make a model editable. On the same page I have tabular inline of a child model. Everything works as expected. Now I want to restrict permission on the tabular inline child form. Specifically remove update and delete permissions on it. I have tried removing the permissions for the admin user using the 'u...

can't figure out serving static images in django dev environment

I've read the article (and few others on the subject), but still can't figure out how to show an image unless a link to a file existing on a web-service is hard-coded into the html template. I've got in urls.py: ... (r'^galleries/(landscapes)/(?P<path>.jpg)$', 'django.views.static.serve', {'document_root': settings.MEDIA_U...

Looping through python-dictionary-turned-into-json in javascript.

In writing a django app, I am returning the following json on a jQuery ajax call: { "is_owner": "T", "author": "me", "overall": "the surfing lifestyle", "score": "1", "meanings": { "0": "something", "1": "something else", "3": "yet something else", "23": "something random" ...

django : Serving static files through nginx

I'm using apache+mod_wsgi for django. And all css/js/images are served through nginx. For some odd, reason when others/friends/colleagues try accessing the site, jquery/css is not getting loaded for them, hence the page looks jumbled up. My html files use code like this - <link rel="stylesheet" type="text/css" href="http://x.x.x.x:8...

Django formset doesn't validate

Hello, I am trying to save a formset but it seems to be bypassing is_valid() even though there are required fields. To test this I have a simple form: class AlbumForm(forms.Form): name = forms.CharField(required=True) The view: @login_required def add_album(request, artist): artist = Artist.objects.get(slug__iexact=artist) A...