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
Hed
Mar 31, 2004

Fun Shoe
I've done it on a few projects and one that my company depends on every day. You definitely aren't alone.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Good to hear it!

Speaking of DRF, apparently Tom Christie started a Kickstarter in the middle of July to fund development of v3 and I didn't even know about it until this afternoon. Anyway, he blew past his goal of £4,000 and reached all the way to £31,043 as of today.

Here's hoping it results in some nice new features in a short-ish timeframe.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Thermopyle posted:

I'm thinking about totally forgoing Django's views and forms and doing everything with django-rest-framework. Anyone else doing this? Stupid idea? Brilliant idea?

My motivation for this is that it seems like a lot of redundant effort maintaing views, forms, serializers, and viewsets when really you could do it all with serializers and viewsets along with JS on the client.

So how would you validate client input? Is this something django-rest-framework does against the model when it handles requests?

Zarithas
Jun 18, 2008
I have a coworker who pronounces Django as "duh-jango" and it's getting pretty aggravating. No matter how many times I, or other coworkers, will say it to him or around him and pronounce it the correct way, he still pronounces the "D".

Somewhat ironically, he also happens to be our team's only Django programmer, though he is a novice one.

He does this with a few other words as well; pronouncing them completely phonetically despite being American and a native English speaker, and despite hearing them the correct way multiple times.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Lumpy posted:

So how would you validate client input? Is this something django-rest-framework does against the model when it handles requests?

Yes.

http://www.django-rest-framework.org/api-guide/serializers.html#validation

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Zarithas posted:

I have a coworker who pronounces Django as "duh-jango" and it's getting pretty aggravating. No matter how many times I, or other coworkers, will say it to him or around him and pronounce it the correct way, he still pronounces the "D".

Somewhat ironically, he also happens to be our team's only Django programmer, though he is a novice one.

He does this with a few other words as well; pronouncing them completely phonetically despite being American and a native English speaker, and despite hearing them the correct way multiple times.

Beat him up imo.

duck monster
Dec 15, 2004

Thermopyle posted:

I'm thinking about totally forgoing Django's views and forms and doing everything with django-rest-framework. Anyone else doing this? Stupid idea? Brilliant idea?

My motivation for this is that it seems like a lot of redundant effort maintaing views, forms, serializers, and viewsets when really you could do it all with serializers and viewsets along with JS on the client.

Django rest framework is slow as a dog. Consider something like tastypie instead.

edit: We're handling giant government datasets at work and stuffing them into EMBs and all sorts of horrible nonsense though so maybe its poor performance on huge datasets might not be so accute to your needs.

duck monster fucked around with this message at 18:24 on Aug 12, 2014

Gounads
Mar 13, 2013

Where am I?
How did I get here?

Thermopyle posted:

I'm thinking about totally forgoing Django's views and forms and doing everything with django-rest-framework. Anyone else doing this? Stupid idea? Brilliant idea?

My motivation for this is that it seems like a lot of redundant effort maintaing views, forms, serializers, and viewsets when really you could do it all with serializers and viewsets along with JS on the client.

Add one more vote for this method. My latest project is angular/dart on the frontend and django/rest on the backend. I've been very happy with it.

I'd recommend it anytime you're doing a complicated client side heavy app.

Chosen
Jul 11, 2002
Vibrates when provoked.

duck monster posted:

Django rest framework is slow as a dog. Consider something like tastypie instead.

edit: We're handling giant government datasets at work and stuffing them into EMBs and all sorts of horrible nonsense though so maybe its poor performance on huge datasets might not be so accute to your needs.

Can you give some more specifics about this performance problem? Where is the slowness, in the serialization? The views are very simple.

I found tastypie's API to be extremely hostile to developers, so I typically go with DRF.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
Any good up to date guides on deploying / hardening a Linux Django install? I would like to deploy to a VPS like Digital Ocean, but I definitely don't want to miss anything security-wise if I go that way.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Here's a tip I just spent two hours figuring out.

When you create model instances in setupClass in django.test.TestCase, those instances never get flushed. You can have unit tests in other files in other apps that are screwed up because those instances still exist until the end of the test runner.

You have to manually flush them in tearDownClass.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Thermopyle posted:

When you create model instances in setupClass in django.test.TestCase, those instances never get flushed.

This is a really common gotcha in Rails too. The transactions wrap individual testcases, not the suite.

Most reasonable db backends support some kind of nested transactions, though, so that seems like something that could be fixed...

KingNastidon
Jun 25, 2004
I'm working on a django app that uses pandas and embedded bokeh plots for portfolio analysis with time series data. I'm looking to move the app to a shared hosting service like webfaction or dreamhost to demo to my bosses and argue my time should be spent on this versus things that actually generate revenue for the company.

My background is in engineering/stats and I have zero experience with the basics of moving an app into production on a non-localhost. What's the process for running the bokeh-server (or any shell process) on a shared host? During local development I'm able to just run the server using command prompt and let it sit idle. Would webfaction's shell access(http://docs.webfaction.com/user-guide/access.html) allow for this? This is probably a super naive question so I'm having a hard time finding relevant examples on searching google. Or for those familiar with Bokeh, is the bokeh-server not meant to be used in this way and I should into something else?

KingNastidon fucked around with this message at 04:46 on Aug 18, 2014

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

KingNastidon posted:

I'm working on a django app that uses pandas and embedded bokeh plots for portfolio analysis with time series data. I'm looking to move the app to a shared hosting service like webfaction or dreamhost to demo to my bosses and argue my time should be spent on this versus things that actually generate revenue for the company.

My background is in engineering/stats and I have zero experience with the basics of moving an app into production on a non-localhost. What's the process for running the bokeh-server (or any shell process) on a shared host? During local development I'm able to just run the server using command prompt and let it sit idle. Would webfaction's shell access(http://docs.webfaction.com/user-guide/access.html) allow for this? This is probably a super naive question so I'm having a hard time finding relevant examples on searching google. Or for those familiar with Bokeh, is the bokeh-server not meant to be used in this way and I should into something else?

You'll probably find deployment a lot easier with something like Heroku.

duck monster
Dec 15, 2004

I've been pretty fond of Docker lately. It seems pretty easy to get up a working docker container and then just poo poo that thing into the 'cloud'. And by 'cloud' I mean any number of services including you own "apt-get install dockerio" setup

ufarn
May 30, 2009
If you know someone decent at Django, have them set up the Heroku Deploy Button for you.

Harik
Sep 9, 2001

From the hard streets of Moscow
First dog to touch the stars


Plaster Town Cop
How do you compose the equivalent of "where butts in ( select farts from .. )", when I already have a queryset for the inner select from another function?

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
Pretty sure that would be:

Crap.objects.filter(butts__in=farts_queryset)

So long as there is a key link between butts and farts.

Harik
Sep 9, 2001

From the hard streets of Moscow
First dog to touch the stars


Plaster Town Cop

Ahz posted:

Pretty sure that would be:

Crap.objects.filter(butts__in=farts_queryset)

So long as there is a key link between butts and farts.

That was it, thanks.

Hanpan
Dec 5, 2004

Just finished my first project using Django CMS 3. Outside of some questionable front-end decisions I really like it, the inline editing is a huge bonus for my clients. Between this and Wagtail, feels good not to have to hack at the django admin panel for simple projects.

Storgar
Oct 31, 2011
I'm looking at Django REST Framework and I think it's pretty good I guess. What do you guys think?

Are there other ways of using Django to create a REST api? Would you recommend against trying to "roll my own"?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Storgar posted:

I'm looking at Django REST Framework and I think it's pretty good I guess. What do you guys think?

Are there other ways of using Django to create a REST api? Would you recommend against trying to "roll my own"?

DRF is awesome, I highly recommend it. Don't roll your own, others have already put a ton of time/effort into figuring things out for us already.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

fletcher posted:

DRF is awesome, I highly recommend it. Don't roll your own, others have already put a ton of time/effort into figuring things out for us already.

I will second this. I implemented an API in tastypie and then redid it in DRF just for the sake of seeing the difference and I found DRF to be much more workable.

Gounads
Mar 13, 2013

Where am I?
How did I get here?

The March Hare posted:

I will second this. I implemented an API in tastypie and then redid it in DRF just for the sake of seeing the difference and I found DRF to be much more workable.

I'll third this?

Just adding that I converted both a piston and a tastypie implementation over. DRF is the way to go.

Storgar
Oct 31, 2011
Wow, sweet. The tutorial and documentation is awesome too. DRF it is!

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I sometimes find myself wishing for Classy Class Based Views, but for DRF.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Anyone know how to integrate bokeh into Django? I know bokeh can do output to static pages, but is there a way to mix its output into a TemplateView's output?

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
ORM and Sequence question:

I have what is essentially a table which is a pool of available codes/sequences for unique keys when I create records elsewhere in the DB.
Right now I run a transaction where I might grab 5000 codes out of an available pool of 1 billion codes using the slice operator [:code_count] where code_count == 5000.

This works fine, but then for every insert, I have to run through each code and insert it into the record manually when I use the code.

Is there a better way?

Example code (omitting other attributes for each new_item that are similar to all new_items):

Python code:
code_count=5000
pool_cds = CodePool.objects.filter(free_indicator=True)[:code_count]

        for pool_cd in pool_cds:
            
            new_item = Item.objects.create(
                pool_cd=pool_cd.unique_code,
            )
            new_item.save()
            
            
		cursor = connection.cursor()
        update_sql = 'update CodePool set free_ind=%s where pool_cd.id in %s'
        instance_param = ()
        
        #Create ridiculously long list of params (5000 items)
        for pool_cd in pool_cds:
            instance_param = instance_param + (pool_cd.id,)
        params = [False, instance_param]
        rows = cursor.execute(update_sql, params)

Ahz fucked around with this message at 17:41 on Sep 4, 2014

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
Nevermind, it works great with

code:
Item.objects.bulk_create([
Item(
pool_cd=pool_cd
) for pool_cd in pool_cds
])
Cut me from about 17 seconds for 2400 records to 2 seconds and it scales better.

Dominoes
Sep 20, 2007

I'm getting duplicates in queries involving many-to-one relationships.

One airfield can have many runways. The query below is returning the Airfield object for each runway that meets the length criteria. Ie if I have an Airfield that has three Runways greater than min_rwy_len, the airfield is in the result 3 times.

Python code:
passed_rwy_len = Airfield.objects.filter(runway__length__gte=min_rwy_len)
Is there a way to avoid this? Can I dedupe it? sort() doesn't work.


edit: running distinct() at the end of the query fixes this.

Dominoes fucked around with this message at 06:33 on Sep 8, 2014

Storgar
Oct 31, 2011
Any recommendations on a json-rpc package?

Thanks for the Django Rest Framework info and recommendation about the docs, by the way.

e. Also I plan to combine it with DRF, so if there's anything I should know that'd be awesome. Thanks!

ee. Hmmm, upon giving it some more thought it looks like DRF should almost cover the server-side of the whole ajax through JSON-RPC schtick. Which just leaves me wondering how I can use javascript to make calls to the api...

Storgar fucked around with this message at 09:12 on Sep 9, 2014

ephphatha
Dec 18, 2009




You could use something like Backbone.js or plain jquery, there's probably a shitload but we use those two at work and they don't make me want to stab my eyes out as much as some of the other code we have.

Dominoes
Sep 20, 2007

I have a basic form view. It's redirecting to the top-level url, (For the test server, http://127.0.0.1:8000/) and therefor returning a Page Not Found error instead of staying on the same url and displaying the result, as it should.

Python code:
def view(request):
    if request.method == 'POST':
        form = DivertForm(request.POST)
        if not form.is_valid():
            return HttpResponse("<h2>Something went wrong - try again.</h2>")

        flight_path = form.cleaned_data['flight_path']
        ...

        divert_fields = diverts_code.find_diverts(flight_path, max_dist, min_rwy_len)

        context = {
            'divert_fields': divert_fields
        }
        return render(request, 'diverts/diverts_results.html', context)

    else:
        form = DivertForm()
        context = {'form': form}
        print('bottom form')

        return render(request, 'diverts/index.html', context)
Nothing in the 'if request.method == 'POST':' block is being executed on form submission. The bottom 'else:' block code gets executed properly when loading the page before submitting.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

Dominoes posted:

I have a basic form view. It's redirecting to the top-level url, (For the test server, http://127.0.0.1:8000/) and therefor returning a Page Not Found error instead of staying on the same url and displaying the result, as it should.

Python code:
def view(request):
    if request.method == 'POST':
        form = DivertForm(request.POST)
        if not form.is_valid():
            return HttpResponse("<h2>Something went wrong - try again.</h2>")

        flight_path = form.cleaned_data['flight_path']
        ...

        divert_fields = diverts_code.find_diverts(flight_path, max_dist, min_rwy_len)

        context = {
            'divert_fields': divert_fields
        }
        return render(request, 'diverts/diverts_results.html', context)

    else:
        form = DivertForm()
        context = {'form': form}
        print('bottom form')

        return render(request, 'diverts/index.html', context)
Nothing in the 'if request.method == 'POST':' block is being executed on form submission. The bottom 'else:' block code gets executed properly when loading the page before submitting.

Post your form html client output. Your action field is probably misdirected.

Dominoes
Sep 20, 2007

Thanks - good call. The problem was in the action field of my HTML template.

Dominoes fucked around with this message at 23:37 on Sep 9, 2014

duck monster
Dec 15, 2004

Ephphatha posted:

You could use something like Backbone.js or plain jquery, there's probably a shitload but we use those two at work and they don't make me want to stab my eyes out as much as some of the other code we have.

Angular.js is great for solving all sorts of problems you don't have. Been forced to use it at work, and its very powerful and all, but I can't help wondering why I'm writing huge steaming piles of boilerplate when 3 lines of jQuery will do.

loving government jobs. :(

Don Mega
Nov 26, 2005
How do you write tests for a form wizard? I need to test the done() function after all the forms are submitted. I'm having trouble finding relevant info on google.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
I'm trying to generate code coverage for my apps and for some reason django_nose and coverage aren't generating reports for my view tests.

My setup.cfg (for nose) looks like this:
INI code:
[nosetests]
with-coverage=1
cover-html=1
exe=1
and my .coveragerc looks like this:
INI code:
[run]
omit=*migrations*
branch=True
The relevant section of my settings file looks like this:
Python code:
INSTALLED_APPS += (
    'django_nose',
)

def get_user_apps():
    from django.db.models.loading import get_apps
    user_apps = [
        a.__name__.split('.')[0]
        for a in get_apps()
        if getenv('VIRTUAL_ENV') not in a.__file__
    ]
    return ','.join(user_apps)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--cover-package=' + get_user_apps(),
]
Code coverage is generated for my app models, signals, and management commands, but for some reason coverage isn't being generated for my app views even though my view tests are being executed.

Am I missing something? The documentation for django_nose and how it interacts with coverage isn't great.

Also, three other related items:
  • I hate using fixtures for testing. I've looked at packages like factory_boy but I can't figure out exactly how I should be using them.
  • I use messages throughout my project and I'm not sure how I should test them. I know messages use the session middleware and I read somewhere that the test client doesn't use middleware. I've seen error messages that confirm this but they randomly went away as I was running tests.
  • Where should I be mocking things in my project? I don't really have any external dependencies and right now I populate my test db with fixtures and creating objects in setUp(). I feel like I might be testing too many things where I could be stubbing or mocking them but I'm honestly not sure where and how I should draw that line.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Blinkz0rz posted:

Also, three other related items:
  • I hate using fixtures for testing. I've looked at packages like factory_boy but I can't figure out exactly how I should be using them.
  • Where should I be mocking things in my project? I don't really have any external dependencies and right now I populate my test db with fixtures and creating objects in setUp(). I feel like I might be testing too many things where I could be stubbing or mocking them but I'm honestly not sure where and how I should draw that line.

I model_mommy instead of factory_boy, but you do something like:

Python code:
widget = mommy.make(WidgetModel)

self.assertTrue(widget.some_bool_returning_method_i_want_to_test())
Anywhere you need to test the value of some field, you just refer to widget.fieldname.

You want to be testing small units of code. So if you have a method that that calls code from somewhere else in your project, mock the return value of that other code. I mean, you don't have to get crazy with it, but python's mock makes mocking pretty easy. You can test that your code calls the remote code by using the Mock.assert_*() methods.

Adbot
ADBOT LOVES YOU

Dominoes
Sep 20, 2007

Is there a way to query for anything, like a wildcard? Improvised method below, looking for something more explicit.

Python code:
if paved_only:
    surface = Q(runway__surface='ASPH') | Q(runway__surface='CONC'))
else:
    surface = ~Q(runway__surface='Unicorn poop')

Airfield.objects.filter(surface, runway__length__gte=8000)

Dominoes fucked around with this message at 13:25 on Sep 12, 2014

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