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
king_kilr
May 25, 2007

nbv4 posted:

since we're talking about extra, does anyone know why this doesn't work?

code:
Flight.objects.all().extra(select={"day": "total - night"}, where=['"day" > 0'])
it just spits out this:

code:
ProgrammingError: column "day" does not exist
LINE 1: ...flight" WHERE "logbook_flight"."user_id" = 3  AND "day" > 0 ...
                                                             ^
but if I leave off the custom 'where' clause, it works fine...

You shouldn't be quoting "day", it's an alias not a DB column.

Adbot
ADBOT LOVES YOU

nbv4
Aug 21, 2002

by Duchess Gummybuns

king_kilr posted:

You shouldn't be quoting "day", it's an alias not a DB column.

Now I get this:

code:
ProgrammingError: column "day" does not exist
LINE 1: ...flight" WHERE "logbook_flight"."user_id" = 3  AND day > 0 OR...
                                                             ^
using postgres 8.4 btw...

king_kilr
May 25, 2007
You may need to reiterate the exacty clause in the where section:
total - night > %s

my SQL is pretty rusty

No Safe Word
Feb 26, 2005

Yup, can't use column aliases from the SELECT portion in the WHERE clause.

http://old.nabble.com/Column-alias-in-where-clause--td18967124.html

nbv4
Aug 21, 2002

by Duchess Gummybuns

No Safe Word posted:

Yup, can't use column aliases from the SELECT portion in the WHERE clause.

http://old.nabble.com/Column-alias-in-where-clause--td18967124.html

yep that was it, good catch.

Now I have another problem. I have a model called "Car". Each car object has one and only one User. Basically I'm trying to find the most common VIN that occurs in the database, across all users:

code:
#cars that have at least one drive in them and don't have a blank vin
c = Car.objects.filter(drive__isnull=False).exclude(vin='').distinct()

#now tell me how many users are connected to each vin
c.annotate(count=Count('vin')).values('count', 'vin').order_by('-count')
But I'm getting incorrect results.

king_kilr
May 25, 2007
You're probably looking for.

code:
Car.objects.exclude(drive=None).exclude(vin='').distinct().values('vin').annotate(count=Count('id')).order_by('-count')

nbv4
Aug 21, 2002

by Duchess Gummybuns

king_kilr posted:

You're probably looking for.

code:
Car.objects.exclude(drive=None).exclude(vin='').distinct().values('vin').annotate(count=Count('id')).order_by('-count')

ah yes thats it. The 'c' query was joining the drive table which was screwing things up

SlightlyMadman
Jan 14, 2005

Anyone else as excited as I am that Dreamhost officially supports Django now? I just set it up the other day and it was absurdly easy and seems to be pretty fast. Kinda funny considering I've set it up manually there with three different accounts in the past couple months, but Passenger WSGI seems much faster than FastCGI so I'm happy to redo it all.

checkeredshawn
Jul 16, 2007

Does anyone know how I could limit the number of objects selected in a ManyToManyField? For example, I have a model with a ManyToManyField and filter_horizontal on that field in the admin page, and I'd like to supply it a number and have it throw an error if I try to add more than that number of objects from the box on the left to the box on the right.

king_kilr
May 25, 2007
You'll need to provide a custom form with your own validation logic: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin

nbv4
Aug 21, 2002

by Duchess Gummybuns
How do get totals by year using the django ORM?

code:
Model.objects.dates('date', 'year').distinct().annotate(total=Sum('value'))
... seems like it should do it but apparently dates() just returns a list.

this works, but feels so wrong:

code:
Model.objects.extra(select={'year':'EXTRACT (YEAR FROM date)' }).values('year').distinct().order_by().annotate(val=Sum('value'))

king_kilr
May 25, 2007
That's pretty much what you have to do, time permitting for Django1.2 I'll be working on the ability to do Model.objects.values("timestamp__year"), but no promises.

nbv4
Aug 21, 2002

by Duchess Gummybuns

king_kilr posted:

That's pretty much what you have to do, time permitting for Django1.2 I'll be working on the ability to do Model.objects.values("timestamp__year"), but no promises.

:patriot:

also, why no django dose last week/this week?

king_kilr
May 25, 2007

nbv4 posted:

:patriot:

also, why no django dose last week/this week?

We recorded both this, and last week. We're working on getting them up, I think last weeks should be going up tonight.

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?
code:
Car.objects.exclude(drive=None).exclude(vin='').distinct().
values('vin').annotate(count=Count('id')).order_by('-count')
code:
Model.objects.extra(select={'year':'EXTRACT (YEAR FROM date)' }).
values('year').distinct().order_by().annotate(val=Sum('value'))
For over ten years I've used SQL without incident. It transferred easily across different programming languages, frameworks, and even databases with relatively minor differences in syntax and capabilities. I could count on one hand the number of times I actually did need to move an application from one database vendor to another, and it was never very difficult to do so. Partly due to consciously using databases in a manner that minimizes lock-in. I never had any complaints about development or maintenance of code that used SQL, it's always just worked.

With Django I started thinking ORMs were interesting, but now I need to be reminded why the concept doesn't cause more problems with obfuscation, indirection, and lock-in than it solves. The above code would be a bigger pain to port from the Django ORM to SQLAlchemy, just within Python, than from one database to another. And that's relevant because I see avoiding framework/library lock-in as important as database lock-in. Possibly more important, because I could see myself someday wanting a change from Django's ORM much sooner than I'd want a change from, say, PostgreSQL which is a much more mature and predictable product.

king_kilr
May 25, 2007
So to be clear code is tied to the APIs you used to write it? Brilliant insight. I'd like my code not to be tied too much to one technology too, it should be painless for me to change all my Python into Haskell easily.

SlightlyMadman
Jan 14, 2005

I agree that the notion I might have to change databases for my project and should be able to do so without any reprogramming is silly. I've only had to do it a few times that I can recall, and even when the projects was hard-coded SQL, the changes were minor and mostly handled by search/replace.

That's not why Django tries to obfuscate the DB, though. The point of a Django app is that I can write one for my site, and you can then take that module and drop it into your site seamlessly. It's not about flexibility so much as reusability.

If you know for sure that your app is custom enough that you won't need to reuse it (or will need to make programming changes anyways if you do), then Django still offers the ability to hard-code your SQL.

chips
Dec 25, 2004
Mein Führer! I can walk!
If you don't want to use the ORM, don't use it? I don't see why you'd complain about choosing to use it if you envisage getting rid of it, presumably because you think it's defective in some way. If you think that writing everything in pure SQL will be easier and lead to less lock-in, do so.

I find the concept of framework portability pretty baffling in itself, since it's not something you'd consider to be an independent component of your software - each one having the same interface, but rather the entire structure it's built around. As king_kilr said, the framework is the API - it don't make sense to have an API for the API on the framework level, you just end up with another framework to be "locked in" to.

That all said, would it be practical to make a generic interface to every different type of framework?

chips fucked around with this message at 18:36 on Nov 18, 2009

MonkeyMaker
May 22, 2006

What's your poison, sir?

SlightlyMadman posted:

That's not why Django tries to obfuscate the DB, though. The point of a Django app is that I can write one for my site, and you can then take that module and drop it into your site seamlessly. It's not about flexibility so much as reusability.

That's not true. You can write a reusable app in Django, yes, but that's not why Django offers an ORM or why you build something in Django as opposed to web.py or CakePHP.

SlightlyMadman
Jan 14, 2005

MonkeyMaker posted:

That's not true. You can write a reusable app in Django, yes, but that's not why Django offers an ORM or why you build something in Django as opposed to web.py or CakePHP.

Cool I'm fairly new to Django so maybe I haven't quite grasped the reasons behind everything yet. Care to elucidate?

MonkeyMaker
May 22, 2006

What's your poison, sir?
You write reusable apps to make your life easier. If you can share it with the community, even better. I don't see it as the goal of Django development, though.

As for using Django over web.py or CakePHP, perhaps you need more power than web.py (easily) provides or you don't want to use CakePHP. CakePHP seems to be a great framework but Mozilla is moving from it to Django for their addons site early next year, so there are obviously some areas where it lacks. Web.py powers quite a few sites but doesn't offer all the features that Django comes with out of the box. If you don't want to roll your own stack, Django is the smarter choice.

SlightlyMadman
Jan 14, 2005

Cool, I've never even looked seriously at the other options you mention (just briefly at CakePHP which struck me as similar but Django seems to have a stronger community).

I was more referring to the "that's not why Django offers an ORM" statement, though. I find the big advantage in of database independence to be that I can port my code from one project to another and not care that they use different databases. That seems like a far greater likelihood to me than the chances that I might suddenly decide to swap out my MySQL database for Oracle without wanting to change my code.

tehk
Mar 10, 2006

[-4] Flaw: Heart Broken - Tehk is extremely lonely. The Gay Empire's ultimate weapon finds it hard to have time for love.
When subclassing a non-abstract model, I know I can get the subclass instance from the superclass instance but I need to know its name. How do I get around that? I am new to django and web apps, so I have a feeling I am doing this wrong, but here is a fictional example
code:
class Achievement(models.Model):
    Completed = models.BooleanField()
    [....]
class GotoNewYork(Achievement):
    [....]
class SwimWithSharks(Achievement):
    [....]

achievements = Achievement.objects.all()
some_achievement = achievements[0]
Now if I know some_achievement is has asuperclass instance of SwimWithSharks I can get the subclass using 'somecity.swimwithsharks', but is there anyway to get the subclass without knowing its name? I know I could keep a list and just see if the variable is in instances namespace, but I figured something like this would have already been implemented.

tehk fucked around with this message at 14:11 on Nov 19, 2009

ATLbeer
Sep 26, 2004
Über nerd
I'm trying to use Django's built in serializer object since I've just been directly calling simplejson forever but, I'm running into some quirks.

I'm going to have a lot of views returning API information and am trying to follow from DRY concepts so I am writing a wrapper to handle the serialization and returning a proper HttpResponse. My problem is that I'm not always returning Django objects. Sometimes I'm just returning normal Python objects. The serializer will choke when it doesn't see Django's _meta tag.

Is there a work around here?

I'd like to use the Django serialization function since I'd like to support all the formats it supports (json, xml, yaml)


----

I'm feeling a little worn out right now so I'm probably explaining this horridly... So hopefully the code will speak for itself. Here is my current 'helper' module and an example implementation

code:
def serializer(obj, format="json"):
    """
        Return a serialized object in specified format        
        >>> test = {}
        >>> test['somevalue'] = "test value"
        >>> test['somearray'] = []
        >>> test['somearray'].append('first value')
        >>> test['somearray'].append('second value')
        >>> test['someint'] = 1
        >>> serializer(test)
        '{"somevalue": "test value", "someint": 1, "somearray": ["first value", "second value"]}'
    """    
    return json.dumps(obj)
    
def HttpServiceResponse(obj, format="json"):
    """
        Return an HttpResponse object with serialized object
        >>> test = {}
        >>> test['somevalue'] = "test value"
        >>> test['somearray'] = []
        >>> test['somearray'].append('first value')
        >>> test['somearray'].append('second value')
        >>> test['someint'] = 1
        >>> HttpServiceResponse(test) #doctest: +ELLIPSIS
        <django.http.HttpResponse object at 0x...>
        
    """
    return HttpResponse(serializer(obj, format))

def HttpServiceErrorResponse(error, format="json"):
    """
        Return an HttpResponse object with serialized error message
        >>> error = "this is a generic error message"
        >>> response = HttpServiceErrorResponse(error) 
        >>> response #doctest: +ELLIPSIS
        <django.http.HttpResponse object at 0x...>
        >>> response.content
        '{"error_message": "this is a generic error message", "error": true}'
    """
    error = {
                'error'         : True,
                'error_message' : error
            }
    return HttpServiceResponse(error, format)
And a related view

code:
def someview(request):
    if check_for_certain_things_that_should_be_right:
        return HttpServiceResponse(#a proper object)
    else:
        return HttpServiceErrorResponse("you must include the POST parameters'")
I'd like to be able to pass any object and any serializable type (json, yaml, xml) to the HttpServiceReponse function and get a proper response in return

ATLbeer fucked around with this message at 20:21 on Nov 19, 2009

Yay
Aug 4, 2007

tehk posted:

When subclassing a non-abstract model [...] is there anyway to get the subclass without knowing its name? I know I could keep a list and just see if the variable is in instances namespace, but I figured something like this would have already been implemented.

As I recall, somthing like this was talked about when ORM model inheritance was first proposed and implemented. It was rejected. Something to do with CORBA (of which I know nothing), I think.

Only 'simple' solution I can think of is using mixins to store the class name, and its backed up by my finding example code here.

tehk
Mar 10, 2006

[-4] Flaw: Heart Broken - Tehk is extremely lonely. The Gay Empire's ultimate weapon finds it hard to have time for love.

Yay posted:

As I recall, somthing like this was talked about when ORM model inheritance was first proposed and implemented. It was rejected. Something to do with CORBA (of which I know nothing), I think.

Only 'simple' solution I can think of is using mixins to store the class name, and its backed up by my finding example code here.
Ah thanks for that, the problem is way bigger than I expected. I will just keep a list of classes which subclass the model as a attribute of Achievement and use a simple method like this to find the correct one
code:
class Achievement(Model):
    [...]
    subclass_list = ["gotonewyork", "swimwithsharks"]

    def get_subclass(self):
        for subclass in self.subclass_list:
            if subclass in dir(self):
                return getattr(self, subclass)
        return None

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Hey guys I was hoping one of you could lead me in the right direction to fixing this.
code:
TypeError at /lugar/1/232/232
view_decorator() got an unexpected keyword argument 'latitud'
Here are the relevant parts.
urls.py
code:
r'^lugar/(?P<bb_serie>.*)/(?P<latitud>.*)/(?P<longitud>.*)', 'myproject.sistema.views.update_coords'),
views.py
code:
@logged_in_or_basicauth
def update_coords(request, bb_serie, latitud, longitud):
    try:
        bb = Blackberry.objects.get(numero_serie = bb_serie)
    except Blackberry.DoesNotExist :
        raise Http404
    
    lugar = Lugar.objects.create(usuario = request.user, bb = bb, latitud = latitud, longitud = longitud)
    return HttpResponse(status=400)
logged_in_or_basicauth I grabbed from here: http://www.djangosnippets.org/snippets/243/

Thanks

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

MEAT TREAT posted:

logged_in_or_basicauth I grabbed from here: http://www.djangosnippets.org/snippets/243/

http://www.djangosnippets.org/snippets/243/ posted:

Comments
Nate (on May 30, 2007):
This is exactly what I needed. In my initial testing it works well but the usage note should be:

@logged_in_or_basicauth()
def your_view:

Note the parentheses. That is because logged_in_or_basicauth is not a decorator but a function which returns a decorator. To specify the HTTP AUTH realm, do this:

@logged_in_or_basicauth('Realm Name')
def your_view:

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Thanks man I hadn't even noticed that all the other functions had the parenthesis and it was driving me nuts. Don't program at 3 in the morning.

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!
I've been using Django for over two years and template tags still drive me loving batty, especially when paired with forms. Can anyone comment on how the hell to encapsulate form processing logic so I don't need to write the same code all over my project? Group post here.

king_kilr
May 25, 2007
What? Why would your form processing logic be anywhere near your template tags? Form processing logic is generally in your views.

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!
Elaborate, please. The form logic isn't specific to any view, so why would it live in one? Are you suggesting that I should duplicate form processing across all views that use the same form?

Edit: Here is my template tag http://dpaste.com/129111/
I'd rather save myself the duplication and cheese the redirect. Surely there's a better way.

Typh fucked around with this message at 18:35 on Dec 4, 2009

MonkeyMaker
May 22, 2006

What's your poison, sir?

Typh posted:

Elaborate, please. The form logic isn't specific to any view, so why would it live in one? Are you suggesting that I should duplicate form processing across all views that use the same form?

Edit: Here is my template tag http://dpaste.com/129111/
I'd rather save myself the duplication and cheese the redirect. Surely there's a better way.

Form validation goes in forms.py. Form processing goes in views.py since you could want it processed differently depending on the 'location' of the save. Template tags are NOT the place for anything like this.

If it's one form that's in multiple spots so you don't want multiple views for processing it, just make one and always direct it to that one. Forms/views from one app can hit views from another app just fine.

The Django docs on forms are actually really good (until you get into formsets, then it's a little wanting). I'd give them another read if I was you.

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!

MonkeyMaker posted:

Form validation goes in forms.py. Form processing goes in views.py since you could want it processed differently depending on the 'location' of the save. Template tags are NOT the place for anything like this.

If it's one form that's in multiple spots so you don't want multiple views for processing it, just make one and always direct it to that one. Forms/views from one app can hit views from another app just fine.

The Django docs on forms are actually really good (until you get into formsets, then it's a little wanting). I'd give them another read if I was you.
But if I use a separate view for processing, what do I do when the form is invalid? I'd need to set up a way to return them back to the view they came from.

Which is something I don't have to do if I use a template tag.

I understand "that's not what template tags are for", but the options presented otherwise do a much worse job of staying generic.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Typh posted:

But if I use a separate view for processing, what do I do when the form is invalid? I'd need to set up a way to return them back to the view they came from.

Which is something I don't have to do if I use a template tag.

I understand "that's not what template tags are for", but the options presented otherwise do a much worse job of staying generic.

If the form is invalid, it'll go back to the URL it was on already, that's how form validation works (at least how it's always worked for me). Also, note that I said form, not save. Those are two separate points of failure.

Saying "it's not generic enough" isn't an excuse for doing it an amazingly wrong way, though. You could pass the current URL along with the form, so you always have the URL to reverse to get the view to render. There are always options.

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!
Form validation doesn't automatically go back to the URL you came from. Usually you call it from the view that loaded it originally anyways, and since you haven't redirected, that's where you end up again.

I'm not making excuses to justify doing it the wrong way, I'm looking for pieces I might be missing to do it cleanly the "right" way.

The URL isn't enough info because I need the objects from the original view to populate the form. Which as far as I can tell leaves me to stuff them in the form or session, which is pretty lame.

I'm trying now to call the form processing view from my original view. Not ideal, but will hopefully cut the duplication down to a single line.

Typh fucked around with this message at 20:36 on Dec 4, 2009

Yay
Aug 4, 2007
Seems to me like the proper way to encapsulate the fact you have multiple functions wanting the same validation is to refactor the template tag into a separate function or class method, and have it called by all the varying object views. Its a supporting method, which is pretty much a definitive use case for either a helper function or a method inside a class object, non?

Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring
I'm using the AuditTrail plugin to keep a detailed history of my model. I want to also know WHO made changes though, and in the Wiki in the Tracking Extra Information section, it mentions how I can have a parameter that calls a function to get extra information such as the current user.

I cannot figure out how to get the current username though. The information is available for views in the request variable, but how can I access it form models.py? I really just need access to the username. The AuditTrail wiki certainly makes it sound like this should be a trivial thing to do but I cannot figure it out.

EDIT: Hey, I figured it out. See here: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

Is uses a middleware solution to make the User object from the request available anywhere. Works perfectly.

Bitruder fucked around with this message at 00:03 on Dec 10, 2009

avidal
May 19, 2006
bawl-some
Anyone currently using Django on Windows (or even Linux) with pyodbc, django-pyodbc, and SQL Server have any experience with lovely performance?

I've been trying to figure out this problem for several days now, but I can't manage it. I created a barebones Django app to try and replicate the problem. If I don't involve the database at all, I get acceptable performance, roughly 400-500 requests/second according to Apache Bench. However, as soon as I involve the db, my performance drops to about 2-5 requests per second. Each individual query completes very quickly (less than a millisecond in some cases), but I still have horrid performance. I've tried with new tables with only one row, tables with 150,000+ rows, it doesn't matter at all.

I'm currently using Django 1.1.1 (but I was using Django 1.1b0 with no difference) as well as the latest pyodbc and django-pyodbc (as well as the legacy django-pyodbc branch) with no differences at all. The problem exists even on CentOS with the same packages.

I'm at my wits end here, I really need to solve this. We are 2 weeks behind deadline because of this problem. I will PayPal anyone that can offer a solution $100.

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007
Your best bet is probably to hope in #django on freenode and message cramm, he's one of the developers of django-pyodbc and he can probably try to help.

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