Re: PostGres 9.5 Upsert

2016-02-01 Thread john . parton
It should be possible to use a "RETURNING" clause to get the new row even 
in the instance of a get_or_create.

I occasionally use an UPDATE ... RETURNING query with Manager.raw to update 
a table and get modified instances in one shot.

On Tuesday, January 12, 2016 at 12:54:03 PM UTC-6, bliy...@rentlytics.com 
wrote:
>
> After thinking about it a bit, I think the only function that would really 
> benefit from this would be the update_or_create.  If you're doing 
> get_or_create you still need a second query to get the actual row.
>
> On Friday, January 8, 2016 at 4:13:26 PM UTC-8, bliy...@rentlytics.com 
> wrote:
>>
>> Hey Guys,
>>
>> Postgres 9.5 has added the functionality for UPSERT aka update or 
>> insert.  Any interest in aligning UPSERT on the db layer with the 
>> get_or_create or update_or_create functionality in django?  Sounds like my 
>> company would be interested in doing the work if the PR will get the 
>> traction.
>>
>> -Ben
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/2b8db682-1b8d-4549-8d8c-f99f3d6b8f90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ForeignKey in Inline generates lots of queries

2014-02-20 Thread john . parton
I posted this to Django users, but didn't get a response. I'd like to help 
to resolve this issue if at all possible.

Previous post:

I have the Django admin configured with a TabularInline, and the inline 
model has a ForeignKey reference to a third model. Every row in the inline 
generates a new query to fetch all of the instances of the third model.

Here's an overview of my code:

# models.py
> from django.db import models
>
> class ExampleParent(models.Model):
> 
> def __unicode__(self):
> return u'Example Parent: %s' % self.id
>
> class ExampleInline(models.Model):
> parent = models.ForeignKey('ExampleParent')
> child = models.ForeignKey('ExampleChild')
> 
> def __unicode__(self):
> return u'Example Inline: %s' % self.id
> 
> class ExampleChild(models.Model):
> 
> def __unicode__(self):
> return u'Example Child: %s' % self.id
>

# admin.py
> from django.contrib import admin
>
> from admin_issue.example_problem.models import (ExampleParent, 
> ExampleInline)
>
> class ExampleInlineInline(admin.TabularInline):
> model = ExampleInline
>
> class ExampleParentAdmin(admin.ModelAdmin):
> inlines = [
> ExampleInlineInline
> ]
>
> admin.site.register(ExampleParent, ExampleParentAdmin) 
>

If you create a bunch of ExampleInlines that reference and ExampleParent, 
then go to that instance in the admin, you get QUERY = u'SELECT 
"example_problem_examplechild"."id" FROM "example_problem_examplechild"' - 
PARAMS = () for every ExampleInline referenced. 

Shouldn't that query be cached by Django? Is that expected behavior? Is 
there a way to force Django to use the cache for subsequent inlines?

I'm also experiencing a similar problem with a ManyToMany field. (But it 
seems to generate even more queries that the ForeignKey)

Any help on this issue would be most appreciated.

Thanks,

John P.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/139e1577-86b2-4d96-9da9-c62d3dba2c75%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.