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
SlightlyMadman
Jan 14, 2005

I haven't used flatpages, but I take it it has a catch-all url? I wrote an app a while ago with a url redirector in it that had a catch-all, and didn't notice I'd included another module in my main urls.py below that one. So yeah, I've done pretty much the same thing before too.

Adbot
ADBOT LOVES YOU

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!

LuckySevens posted:

I'm a moron. I just spent 2 hours trying to get a custom comment system preview to display properly. I couldn't work out why it was giving me a 404. I tried everything, even ported it into another project and somehow it worked there. I made a post here with all the info, and it wasn't until I was seconds away from posting that I saw it.

I put the urlpattern under my flatpages. please tell me im not the only one to have done this :(
At least once a month, my friend, at least once a month.

Ethereal
Mar 8, 2003
So far, I'm loving this framework, it's ridiculously easy to create a website, no matter how complex.

I am having an issue though. I'd like to serialize an object returned from my database into json format. I've got that figured out using the serializers class. But I have a foreign key that I'd like to translate and have inserted in the final json object. I thought annotations would be perfect for this but I can't seem to annotate on a foreign key. Does anyone have an example of how I can do this?

king_kilr
May 25, 2007
http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers

Should help with serializing related models.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

king_kilr posted:

http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers

Should help with serializing related models.

I've used this, and it's pretty good. You'll get a few errors: one I remember was about him using the 'newforms' name instead of just 'forms' - probably because it was written before 1.x - but it's literally just a case of a quick edit to the source to make it sensible again. Oh, and the JSON format's a little weird, but easy enough to figure out from looking at it.

Ethereal
Mar 8, 2003

king_kilr posted:

http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers

Should help with serializing related models.

I hope this becomes a part of the main branch because it's loving awesome. Thank you so much!

sink
Sep 10, 2005

gerby gerb gerb in my mouf
Is it possible to filter by the ManyToMany fields?

Suppose I have a Pizza model and a Topping model. Topping has a ManyToManyField pointing to Pizza. I now have an instance of a Pizza called mypizza.

I would like to now search for all Pizzas with the EXACT same Toppings as mypizza. Intuitively it would like something like this:

Pizza.objects.filter(toppings=mypizza.toppings_set)

This, naturally, does not work. Any ideas? I'm happy to do some fancy black magic with the models to make it work, or do a custom SQL clause. I just don't know enough SQL to do that.

Thanks!

jupo
Jun 12, 2007

Time flies like an arrow, fruit flies like a banana.
After reading the docs you'll probably come up with something like Pizza.objects.filter(toppings__in=mypizza.toppings_set.all())

sink
Sep 10, 2005

gerby gerb gerb in my mouf
I tried that. IN does not mean two sets are equal.

Or, to be snarky: After reading the docs you'll find that the IN operator does not do what I want. :)

nbv4
Aug 21, 2002

by Duchess Gummybuns

sink posted:

I tried that. IN does not mean two sets are equal.

Or, to be snarky: After reading the docs you'll find that the IN operator does not do what I want. :)

If I were you, I'd just make a function that makes a string representation of the toppings, something that outputs something like "6532,1245,472,124" (the pk's of the toppings objects), and then call that function in the save() method, and store it in a CharField. Then you can just do:

Pizza.objects.filter(toppings_flat=mypizza.flatten_toppings())

sink
Sep 10, 2005

gerby gerb gerb in my mouf
This seems like a good idea but it just ... feels wrong. As though it were contrary to the way relational databases should be used. Am I stupid in thinking this way? Is this a common practice? My database architecture skills are very lacking. Thanks! I might try it.

nbv4
Aug 21, 2002

by Duchess Gummybuns

sink posted:

This seems like a good idea but it just ... feels wrong. As though it were contrary to the way relational databases should be used. Am I stupid in thinking this way? Is this a common practice? My database architecture skills are very lacking. Thanks! I might try it.

well, if theres no way to do it with the django ORM, then you have no other choice. This technique is called "denormalization" and will actually be a lot faster than doing the join, especially if you'll be doing a lot of these queries.

SlightlyMadman
Jan 14, 2005

Yeah, if I were custom coding something like that, that's probably exactly how I'd do it. I can't think of any way to do it is SQL that would be nearly as fast.

sink
Sep 10, 2005

gerby gerb gerb in my mouf
I feel pretty retarded that this wasn't so obvious to me.

Extending my example where I am searching for pizzas with identical toppings to mypizza.

I just need to find pizzas with the same number of toppings as mypizza and then make sure that all of the toppings in mypizza are in those result pizzas.

While you still can't do this with the Django ORM it's not super hard to do with SQL in a single statement using COUNT and IN on the Django-created pizza_topping join table... I think.

king_kilr
May 25, 2007

sink posted:

I feel pretty retarded that this wasn't so obvious to me.

Extending my example where I am searching for pizzas with identical toppings to mypizza.

I just need to find pizzas with the same number of toppings as mypizza and then make sure that all of the toppings in mypizza are in those result pizzas.

While you still can't do this with the Django ORM it's not super hard to do with SQL in a single statement using COUNT and IN on the Django-created pizza_topping join table... I think.

You can do it with the Django ORM, and I seriously don't know what everyone else in this thread is suggesting, denormalization is not the solution here:
code:
qs = Pizza.objects.annotate(toping_count=Count("toppings")).filter(toping_count=my_pizza.topings.count())
for toping in my_pizza.toppings.all():
    qs = qs.filter(topings=toping)
Tada

nbv4
Aug 21, 2002

by Duchess Gummybuns
it depends on how you have your database set up. If you have a few hundred thousand pizzas and more than a few hundred toppings, the above queryset will slow things down a lot. Me, personally, I try to avoid any kind of full-table annotate like that unless it's a query that only gets executed once a day or something. Also, "denormalization" isn't really the right word. You're not denormalizing all of your data, you're just denormalizing for that one query.

sink
Sep 10, 2005

gerby gerb gerb in my mouf
king_kilr,

Thanks! The annotate was the key.

If efficiency becomes an issue I will try something else out.

How common is it for you guys to optimize parts of your webapp outside of the ORM? It seems like Django's object model isn't always the most efficient or intuitive way to work with tables. Does anyone ever supplement the ORM with SQL Alchemy for particularly hairy transactions?

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

nbv4 posted:

it depends on how you have your database set up. If you have a few hundred thousand pizzas and more than a few hundred toppings, the above queryset will slow things down a lot. Me, personally, I try to avoid any kind of full-table annotate like that unless it's a query that only gets executed once a day or something.


What? No it doesn't, at least not that annotate (Well, at least not on DB2, MySQL, PostgreSQL or Oracle, ymmv in MSSQL***). Unless your indexes are retarded. If you have a several million pizzas you'll have a different issue.

As always the trick is to get django's query for the operation and index appropriately (I think this particular query might benefit from a compound index) after an EXPLAIN/ANALYZE.

quote:

Also, "denormalization" isn't really the right word. You're not denormalizing all of your data, you're just denormalizing for that one query.

Denormalization is the correct word. Denormalization is not always done on your entire data set, but on some fields that. In this case it might be useful since pizza toppings don't change, but, again, you won't run into problems until the several millions, and even then I bet you'd have to do some fancy denormalization on other parts of your data set before this particular query becomes a problem.


*** Specially not in DB2. And actually over time PostgreSQL and Oracle generate bitmap indexes on the fly too, so no index needed there. Pretty sure DB2 also does this.

deimos fucked around with this message at 13:38 on Dec 28, 2009

king_kilr
May 25, 2007
I've never needed to replace Django's ORM. Of course I also know the deep dark internals and don't mind messing down there when necessary. That being said I do find Django's ORM incredibly intuitive, I wouldn't love it so much if it didn't make me so happy.

Malcolm Treddinick actually wrote a blog post on how to solve this issue, but his site is down ATM :(

Sock on a Fish
Jul 17, 2004

What if that thing I said?
I'm trying to deploy with WSGI and am apparently an idiot. I've got a test WSGI script that will return an HTTP response just fine, but when I try to access my app I get 403'd. I've verified that filesystem perms are appropriate, and have verified that I can run the dev server on the production platform just in case some dependency was missing. When I call my Django WSGI on the command line it runs and exits clean after a couple of seconds.

I think there must be something wrong with my Apache config. Here it is:
code:
<VirtualHost *:80>
ServerName somedomain.org

ErrorLog /var/log/httpd/somedomain.org-error_log
CustomLog /var/log/httpd/somedomain.org-access_log common


Alias /m /home/django/apps/myapp/r/

WSGIScriptAlias / /home/django/apps/myapp/apache/myapp.wsgi
WSGIDaemonProcess django processes=7 threads=1 display-name=django
WSGIProcessGroup django
WSGIApplicationGroup django

ErrorDocument 401 "Authentication Error"
ErrorDocument 403 "Forbidden"

<Directory /home/django/apps/myapp/apache>
   Order deny,allow
   Allow from all
</Directory>

<Directory /home/django/apps/myapp/r>
   Order deny,allow
   Allow from all
</Directory>

</VirtualHost>
I've tried commenting out the ErrorDocument directives to get better insight, I just get told by Apache that I don't have permissions to access whatever URL it is I'm trying to retrieve.

Sock on a Fish
Jul 17, 2004

What if that thing I said?

Sock on a Fish posted:


I got this working, but I'm not sure why it was broken. I moved the app out of the /home/django dir and all is now well. Perms were set 644/755 for everything in my app's directory.

Yay
Aug 4, 2007
Did you get anything better in the error logs? My gut says that it was probably wsgi not able to write the socket in the directory, but I can't remember if that results in a ISE 500 or a 401.

bmoyles
Feb 15, 2002

United Neckbeard Foundation of America
Fun Shoe
What are perms of the myapp and the /home/django directory? Many distros, when you create users, remove execute or read permissions on home directories. If /home/django was missing +x or /home/django/myapp was missing +x, the apache user wouldn't be able to change into that directory.

Sock on a Fish
Jul 17, 2004

What if that thing I said?

bmoyles posted:

What are perms of the myapp and the /home/django directory? Many distros, when you create users, remove execute or read permissions on home directories. If /home/django was missing +x or /home/django/myapp was missing +x, the apache user wouldn't be able to change into that directory.

Ah, that probably would've been it then. I didn't think it mattered what the permissions of a directory many levels above my WSGI script were.

Entry in error log was simply:
code:
[Sun Dec 27 12:34:20 2009] [error] [client 4.4.4.4] (13)Permission denied: access to / denied
In access log:
code:
4.4.4.4 - - [27/Dec/2009:12:49:22 -0800] "GET / HTTP/1.1" 403 282

nbv4
Aug 21, 2002

by Duchess Gummybuns
just a heads up yall, the djangocon videos have been finally posted here: http://djangocon.blip.tv/

Mulozon Empuri
Jan 23, 2006

nbv4 posted:

just a heads up yall, the djangocon videos have been finally posted here: http://djangocon.blip.tv/

Awesome! I've been waiting ages for those to surface.

Django 1.2 alpha 1 looks excellent as well. Good times.

bmoyles
Feb 15, 2002

United Neckbeard Foundation of America
Fun Shoe
So here's a funky kind of question...
We're trying to segregate static media from dynamic media within our application (static being things that are deployed wholesale with the application, dynamic being things that can be managed through the Django admin).

In order to do this, we created two new settings alongside MEDIA_URL and MEDIA_ROOT, RWMEDIA_URL and RWMEDIA_ROOT.

In our models, we're creating a FileSystemStorage object:
rwstorage = FileSystemStorage(location=settings.RWMEDIA_ROOT, base_url=settings.RWMEDIA_URL)
and then using that in models that have file or image upload fields like so:

class AvatarImage(models.Model):
image = models.ImageField(storage=rwstorage, upload_to="images/avatar/")
name = models.CharField(max_length=32)
def __url__(self):
return self.image

Problem is, this doesn't allow for relative urls near as I can tell, as the FileSystemStorage class' function relies upon urlparse.urljoin, which requires a full URL (http://...) for its base URL, meaning I can't generate relative URLs, correct?

Any ideas on how to get around this, or are we stuck assembling base_url as an absolute URL?

bmoyles
Feb 15, 2002

United Neckbeard Foundation of America
Fun Shoe
Hmm, so it looks like all we had to do was append a trailing / to the MEDIA_URL and RWMEDIA_URL settings, and everything works. Means we'll have some funky double slashes in some constructed URLs, and it sounds like it might've broken one of our context handlers, but I think we can work around this for now.

Doh004
Apr 22, 2007

Mmmmm Donuts...
I'm having trouble getting my urls to read in a slug:

code:
(r'^portfolio/(?P<slug>\w+)/$','testproject.portfolio.views.viewjob')
And when I goto /portfolio/known-slug/ it comes up saying it couldn't match any of expressions.

What am I doing wrong? :(

*edit* Wait! When I put in the slug without the -, it at least matched up. How do I allow hyphens?

*double edit* Nevermind, I got it. Had to change the w to [-\w]

Doh004 fucked around with this message at 00:06 on Jan 14, 2010

Doh004
Apr 22, 2007

Mmmmm Donuts...
Sorry for the double post but I have another question.

I've installed the django-tinymce and was able to implement it by changing my CharField to a HTMLField.

I want to change the body/content of a flatpage to incorporate the TinyMCE. I'm pretty sure I could accomplish this by defining a FlatPageAdmin class and unregister the FlatPage and reregister it with said class, but which admin.py would I put this in? Can I make an admin.py outside of an individual app and just at my project root? What exactly would be the best/easiest way to do this? I've found a lot of tutorials online, but they're all really outdated and I don't fully trust them.

Thanks.

king_kilr
May 25, 2007

Doh004 posted:

Sorry for the double post but I have another question.

I've installed the django-tinymce and was able to implement it by changing my CharField to a HTMLField.

I want to change the body/content of a flatpage to incorporate the TinyMCE. I'm pretty sure I could accomplish this by defining a FlatPageAdmin class and unregister the FlatPage and reregister it with said class, but which admin.py would I put this in? Can I make an admin.py outside of an individual app and just at my project root? What exactly would be the best/easiest way to do this? I've found a lot of tutorials online, but they're all really outdated and I don't fully trust them.

Thanks.

Yes, you can make an admin.py anywhere, and as long as it's processed (read: imported) you'll be ok. My solution would just be to add a flatpage_ext app with nothing but the admin.py, but that's me. Otherwise you're spot on about providing a new ModelAdmin for FlatPage.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Anyone have a good guide to having multiple levels of user authentication? ie, premium account and free accounts? Or am I just missing the obvious documentation on it?

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?

LuckySevens posted:

Anyone have a good guide to having multiple levels of user authentication? ie, premium account and free accounts? Or am I just missing the obvious documentation on it?

Look at the section "Storing additional information about users" here: http://docs.djangoproject.com/en/1.1/topics/auth/

Or you could use permission groups.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

jesus how did i overlook groups, time for a coffee.

SlightlyMadman
Jan 14, 2005

I set up a site that uses an email address for a username, and while I know you're not supposed to do that, it vastly simplifies the registration process for the user, so I did it anyways. I have no real problems on the registration and login side, but I can't edit any accounts in the admin console, because I get validation errors.

What's the right way to do this? I also need to alter the user model, since I've discovered the username is limited to 30 characters, and I've already had one user unable to register since he had a preposterously long email address.

Would a better approach be to just leave it alone, and toss a random alphanumeric string in the username column upon registration, then override all the authorization methods to check against email instead of username? I can't tell if that's a cleaner solution, or just more of a hack.

chips
Dec 25, 2004
Mein Führer! I can walk!

SlightlyMadman posted:

I set up a site that uses an email address for a username, and while I know you're not supposed to do that, it vastly simplifies the registration process for the user, so I did it anyways. I have no real problems on the registration and login side, but I can't edit any accounts in the admin console, because I get validation errors.

What's the right way to do this? I also need to alter the user model, since I've discovered the username is limited to 30 characters, and I've already had one user unable to register since he had a preposterously long email address.

Would a better approach be to just leave it alone, and toss a random alphanumeric string in the username column upon registration, then override all the authorization methods to check against email instead of username? I can't tell if that's a cleaner solution, or just more of a hack.

I've got this issue too, unfortunately on a live site. Is there any way we could hack around to disrupt the checking on the admin site?

Mulozon Empuri
Jan 23, 2006

I did this: http://dpaste.com/hold/147468/ to be able to edit accounts with רזו in the usernames. Too tired to figure out if that's any use to you guys, but there you go.

king_kilr
May 25, 2007

chips posted:

I've got this issue too, unfortunately on a live site. Is there any way we could hack around to disrupt the checking on the admin site?

I've got a site with this as well, although I have no idea if our admin works. You should just need to provide a custom authentication backend that auths by email instead of username.

SlightlyMadman
Jan 14, 2005

Mulozon Empuri posted:

I did this: http://dpaste.com/hold/147468/ to be able to edit accounts with רזו in the usernames. Too tired to figure out if that's any use to you guys, but there you go.

This looks like it could do the trick. Pardon if this is a stupid question, but where do I put that? Can I just drop it in my app's admin.py file, or do I need to put it somewhere special? Additionally, is there a place I can override the User model to increase its max length?

Adbot
ADBOT LOVES YOU

chips
Dec 25, 2004
Mein Führer! I can walk!

Mulozon Empuri posted:

I did this: http://dpaste.com/hold/147468/ to be able to edit accounts with רזו in the usernames. Too tired to figure out if that's any use to you guys, but there you go.

Cheers, that looks like about the right thing.

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