Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Johannes Schneider

thnx guys for all that input.

On 21.01.2014 14:06, Erik Cederstrand wrote:

Den 21/01/2014 kl. 10.55 skrev Johannes Schneider 
:


yes, but the problem is that we manually give them a pk.

Maye it's the best to reduce the question to the following:
Which (undocumented) field is better for this case?
* instance._state.db (checking if this is None)
* instance_state.adding (checking for True)


Pick whichever works for you. But make sure to write tests for this, so it 
still works when you upgrade Django.

Erik




--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52DE764A.5020609%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Erik Cederstrand
Den 21/01/2014 kl. 10.55 skrev Johannes Schneider 
:

> yes, but the problem is that we manually give them a pk.
> 
> Maye it's the best to reduce the question to the following:
> Which (undocumented) field is better for this case?
> * instance._state.db (checking if this is None)
> * instance_state.adding (checking for True)

Pick whichever works for you. But make sure to write tests for this, so it 
still works when you upgrade Django.

Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ADA379C6-E84C-492F-AFEE-7148CC9545A3%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Avraham Serour
pk is not necessarily id, in any case if you are the one adding it then you
already know that the object didn't come from the db, in order to check
that later I would add an attribute to the object and check it's presence,
if not present it means it came from the db.


On Tue, Jan 21, 2014 at 11:55 AM, Johannes Schneider <
johannes.schnei...@galileo-press.de> wrote:

> yes, but the problem is that we manually give them a pk.
>
> Maye it's the best to reduce the question to the following:
> Which (undocumented) field is better for this case?
> * instance._state.db (checking if this is None)
> * instance_state.adding (checking for True)
>
> bg,
> Johannes
>
>
>
> On 21.01.2014 10:07, Avraham Serour wrote:
>
>> wouldn't checking for the presence of an id enough? as far as I know
>> instantiated objects don't have id's
>>
>>
>> On Tue, Jan 21, 2014 at 10:16 AM, Erik Cederstrand
>> > wrote:
>>
>> Den 21/01/2014 kl. 08.58 skrev Johannes Schneider
>> > >:
>>
>>
>>  > maybe I I formulated it not precise enough.
>>  > I need to check if some instance of a model comes from some
>> database query (e.g. via get(..)) or is instanciated 'by hand'. and
>> not jet written to the database.
>>  > In this case I don't need to know, if there is an related object
>> (e.g. with the same pk or same values for its attributes) in the
>> database.
>>
>> Okay. Sounds like your best bet is to either roll your own, or use
>> the undocumented API as suggested by Tom Evans.
>>
>> Either way, make sure to write tests so this doesn’t break when you
>> upgrade Django.
>>
>> Erik
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to django-users+unsubscr...@googlegroups.com
>> .
>>
>> To post to this group, send email to django-users@googlegroups.com
>> .
>>
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/78B3DC94-
>> CBFC-4F38-A35C-73F54CAB572A%40cederstrand.dk.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send
>> an email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAFWa6t%
>> 2Brm9REWaWUTqZHd4Wv3Pw212RLGSh89%2BWJwC7NFNM%3DqQ%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
> --
> Johannes Schneider
> Webentwicklung
> johannes.schnei...@galileo-press.de
> Tel.: +49.228.42150.xxx
>
> Galileo Press GmbH
> Rheinwerkallee 4 - 53227 Bonn - Germany
> Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
> http://www.galileo-press.de/
>
> Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
> HRB 8363 Amtsgericht Bonn
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/52DE442B.1010300%40galileo-press.de.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFWa6t%2BNnHb94_vNdFoKv8bsv_cF3pzxt2MpuftKir5WcV67rA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Johannes Schneider

yes, but the problem is that we manually give them a pk.

Maye it's the best to reduce the question to the following:
Which (undocumented) field is better for this case?
* instance._state.db (checking if this is None)
* instance_state.adding (checking for True)

bg,
Johannes


On 21.01.2014 10:07, Avraham Serour wrote:

wouldn't checking for the presence of an id enough? as far as I know
instantiated objects don't have id's


On Tue, Jan 21, 2014 at 10:16 AM, Erik Cederstrand
> wrote:

Den 21/01/2014 kl. 08.58 skrev Johannes Schneider
>:

 > maybe I I formulated it not precise enough.
 > I need to check if some instance of a model comes from some
database query (e.g. via get(..)) or is instanciated 'by hand'. and
not jet written to the database.
 > In this case I don't need to know, if there is an related object
(e.g. with the same pk or same values for its attributes) in the
database.

Okay. Sounds like your best bet is to either roll your own, or use
the undocumented API as suggested by Tom Evans.

Either way, make sure to write tests so this doesn’t break when you
upgrade Django.

Erik

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to django-users+unsubscr...@googlegroups.com
.
To post to this group, send email to django-users@googlegroups.com
.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit

https://groups.google.com/d/msgid/django-users/78B3DC94-CBFC-4F38-A35C-73F54CAB572A%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAFWa6t%2Brm9REWaWUTqZHd4Wv3Pw212RLGSh89%2BWJwC7NFNM%3DqQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52DE442B.1010300%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Avraham Serour
wouldn't checking for the presence of an id enough? as far as I know
instantiated objects don't have id's


On Tue, Jan 21, 2014 at 10:16 AM, Erik Cederstrand <
erik+li...@cederstrand.dk> wrote:

> Den 21/01/2014 kl. 08.58 skrev Johannes Schneider <
> johannes.schnei...@galileo-press.de>:
>
> > maybe I I formulated it not precise enough.
> > I need to check if some instance of a model comes from some database
> query (e.g. via get(..)) or is instanciated 'by hand'. and not jet written
> to the database.
> > In this case I don't need to know, if there is an related object (e.g.
> with the same pk or same values for its attributes) in the database.
>
> Okay. Sounds like your best bet is to either roll your own, or use the
> undocumented API as suggested by Tom Evans.
>
> Either way, make sure to write tests so this doesn’t break when you
> upgrade Django.
>
> Erik
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/78B3DC94-CBFC-4F38-A35C-73F54CAB572A%40cederstrand.dk
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFWa6t%2Brm9REWaWUTqZHd4Wv3Pw212RLGSh89%2BWJwC7NFNM%3DqQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-21 Thread Erik Cederstrand
Den 21/01/2014 kl. 08.58 skrev Johannes Schneider 
:

> maybe I I formulated it not precise enough.
> I need to check if some instance of a model comes from some database query 
> (e.g. via get(..)) or is instanciated 'by hand'. and not jet written to the 
> database.
> In this case I don't need to know, if there is an related object (e.g. with 
> the same pk or same values for its attributes) in the database.

Okay. Sounds like your best bet is to either roll your own, or use the 
undocumented API as suggested by Tom Evans.

Either way, make sure to write tests so this doesn’t break when you upgrade 
Django.

Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/78B3DC94-CBFC-4F38-A35C-73F54CAB572A%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-20 Thread Johannes Schneider

maybe I I formulated it not precise enough.
I need to check if some instance of a model comes from some database 
query (e.g. via get(..)) or is instanciated 'by hand'. and not jet 
written to the database.
In this case I don't need to know, if there is an related object (e.g. 
with the same pk or same values for its attributes) in the database.


bg,
Johannes

On 20.01.2014 18:21, Avraham Serour wrote:

well you have a problem then, the database is the one holding the data,
if you want to know if the data exists will would need to ask the one
responsible for it, if not the DB then who?

you could keep a copy in memory but then you will have two problems. are
you actually having problems with exists() or is this just premature
optimization?


On Mon, Jan 20, 2014 at 6:13 PM, Johannes Schneider
> wrote:

I don't do, because I don't want do to have a query against the
database.


On 20.01.2014 16:25, Kelly Nicholes wrote:

Wait-- Why aren't you using exists()?  Don't even check the pk.
  Model.query.exists() was made for this.



--
Johannes Schneider
Webentwicklung
johannes.schneider@galileo-__press.de

Tel.: +49.228.42150 .xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0  (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to django-users+unsubscribe@__googlegroups.com
.
To post to this group, send email to django-users@googlegroups.com
.
Visit this group at http://groups.google.com/__group/django-users
.
To view this discussion on the web visit

https://groups.google.com/d/__msgid/django-users/52DD4B1B.__9020508%40galileo-press.de

.

For more options, visit https://groups.google.com/__groups/opt_out
.


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



--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52DE28A9.8060902%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-20 Thread Avraham Serour
well you have a problem then, the database is the one holding the data, if
you want to know if the data exists will would need to ask the one
responsible for it, if not the DB then who?

you could keep a copy in memory but then you will have two problems. are
you actually having problems with exists() or is this just premature
optimization?


On Mon, Jan 20, 2014 at 6:13 PM, Johannes Schneider <
johannes.schnei...@galileo-press.de> wrote:

> I don't do, because I don't want do to have a query against the database.
>
>
> On 20.01.2014 16:25, Kelly Nicholes wrote:
>
>> Wait-- Why aren't you using exists()?  Don't even check the pk.
>>  Model.query.exists() was made for this.
>>
>>
>
> --
> Johannes Schneider
> Webentwicklung
> johannes.schnei...@galileo-press.de
> Tel.: +49.228.42150.xxx
>
> Galileo Press GmbH
> Rheinwerkallee 4 - 53227 Bonn - Germany
> Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
> http://www.galileo-press.de/
>
> Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
> HRB 8363 Amtsgericht Bonn
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/52DD4B1B.9020508%40galileo-press.de.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

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


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-20 Thread Johannes Schneider

I don't do, because I don't want do to have a query against the database.

On 20.01.2014 16:25, Kelly Nicholes wrote:

Wait-- Why aren't you using exists()?  Don't even check the pk.  
Model.query.exists() was made for this.




--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52DD4B1B.9020508%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-20 Thread Kelly Nicholes
Wait-- Why aren't you using exists()?  Don't even check the pk.  
Model.query.exists() was made for this.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2bccd6fc-e628-4b66-8299-6516904073d2%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-20 Thread Johannes Schneider
thnx for this suggestion. we use '_state.adding' now. Is there an 
advantage of using '_state.database'?


bg,
Johannes

On 17.01.2014 14:39, Tom Evans wrote:

On Thu, Jan 16, 2014 at 5:23 PM, Johannes Schneider
 wrote:

This could be a way to it, but we need this information while handling the
Pre_save singal. So this does not work too.


Abuse the django model instance's internals and look to see what
database this instance was loaded from - if it is None, it hasn't come
from the database.

This is highly fragile - using an undocumented API that I know has
changed in recent versions. Personally I would test that the pk does
not exist in the database (and buy faster hardware.)



u=User.objects.get(id=1)
u2=User()
u._state.db

'default'

u2._state.db



Cheers

Tom




--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52DCDA7B.4000206%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-17 Thread Tom Evans
On Thu, Jan 16, 2014 at 5:23 PM, Johannes Schneider
 wrote:
> This could be a way to it, but we need this information while handling the
> Pre_save singal. So this does not work too.

Abuse the django model instance's internals and look to see what
database this instance was loaded from - if it is None, it hasn't come
from the database.

This is highly fragile - using an undocumented API that I know has
changed in recent versions. Personally I would test that the pk does
not exist in the database (and buy faster hardware.)


>>> u=User.objects.get(id=1)
>>> u2=User()
>>> u._state.db
'default'
>>> u2._state.db
>>>

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1JYsE58wL9phRtabVN1RTcEL07PBGwnHHhPE6uUNi2y%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-17 Thread Erik Cederstrand
Den 16/01/2014 kl. 17.50 skrev Johannes Schneider 
:

> The point is, I have to distinguish between an instance which is created, but 
> its save method is not yet called. And an instance coming from the database 
> via a manager.

Okay, then create an instance variable on the model instances when you create 
them manually. Something like this:

obj = InstanceModel()
obj.pk = 123
obj.manually_created = True

Then have your save() method check for this variable:

 def save(self, *args, **kwargs):
if getattr(self, ‘manually_created’, False):
   # Was created manually. Do something
else:
   # From database. Do something else
super(self, InstanceModel).save(*args, **kwargs)
del obj.manually_created


Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/BA80A159-B4D0-4559-98E5-779EFE6717A7%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Johannes Schneider
No, just if you use auto generated PKs. Exactly this is the point. We 
use manually generated PKs, so can not use this check.



On 16.01.2014 18:03, Javier Guerra Giraldez wrote:

On Thu, Jan 16, 2014 at 11:50 AM, Johannes Schneider
 wrote:

The point is, I have to distinguish between an instance which is created,
but its save method is not yet called. And an instance coming from the
database via a manager.



AFAIK, if it has a 'pk' property, it has been saved.




--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52D8155C.8060307%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Johannes Schneider
This could be a way to it, but we need this information while handling 
the Pre_save singal. So this does not work too.


On 16.01.2014 17:53, Martin Marrese wrote:

On Thu, Jan 16, 2014 at 1:50 PM, Johannes Schneider
> wrote:

The point is, I have to distinguish between an instance which is
created, but its save method is not yet called. And an instance
coming from the database via a manager.

bg,
Johannes


Perhaps get_or_create
(https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create)
?

Martín

--
Martin Marrese
marr...@gmail.com 
+54 911 5112 9448

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAKQdVACvdMxrQERzB18dBDXPT4bDMXWOuV5NXpkE68eX-rfL9A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52D8159A.5030105%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Javier Guerra Giraldez
On Thu, Jan 16, 2014 at 11:50 AM, Johannes Schneider
 wrote:
> The point is, I have to distinguish between an instance which is created,
> but its save method is not yet called. And an instance coming from the
> database via a manager.


AFAIK, if it has a 'pk' property, it has been saved.

-- 
Javier

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFkDaoSOhujBWX2XJuD6FH7CgQFxZXZRJ7%2BD2Y7imP5uF3HmnQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Martin Marrese
On Thu, Jan 16, 2014 at 1:50 PM, Johannes Schneider <
johannes.schnei...@galileo-press.de> wrote:

> The point is, I have to distinguish between an instance which is created,
> but its save method is not yet called. And an instance coming from the
> database via a manager.
>
> bg,
> Johannes
>

Perhaps get_or_create (
https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create) ?

Martín

-- 
Martin Marrese
marr...@gmail.com
+54 911 5112 9448

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKQdVACvdMxrQERzB18dBDXPT4bDMXWOuV5NXpkE68eX-rfL9A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Johannes Schneider
The point is, I have to distinguish between an instance which is 
created, but its save method is not yet called. And an instance coming 
from the database via a manager.


bg,
Johannes

On 16.01.2014 16:04, Erik Cederstrand wrote:

Den 16/01/2014 kl. 14.27 skrev Johannes Schneider 
:


Hi List,
I need to check, if a given instance of a Model already exists in the Database. 
I cannot check 'instance.pk is None', because there are manually given PKs (no 
discussion about this plz).

Up to now I check if 'instance._state.adding' is true. But I don't like this 
solution.
Checking 'InstanceModel.objects.exists(pk=self.pk)' generates too much queries, 
so this is not acceptable in my case.


Can you elaborate on “too much queries”? exists() is supposed to be the 
cheapest way of checking if the object exists. It seems like you want to check 
the database, without actually checking the database…

If you want to check a large number of objects at the same time, just do 
something like:

InstanceModel.objects.filter(id__in=(your, list, of, 
PKs)).values_list('id', flat=True)

to get all pre-existing PKs in one query.

Erik




--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52D80DDF.90004%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Fast and Easy way to check if an instance of an Model exists in the Database

2014-01-16 Thread Erik Cederstrand
Den 16/01/2014 kl. 14.27 skrev Johannes Schneider 
:

> Hi List,
> I need to check, if a given instance of a Model already exists in the 
> Database. I cannot check 'instance.pk is None', because there are manually 
> given PKs (no discussion about this plz).
> 
> Up to now I check if 'instance._state.adding' is true. But I don't like this 
> solution.
> Checking 'InstanceModel.objects.exists(pk=self.pk)' generates too much 
> queries, so this is not acceptable in my case.

Can you elaborate on “too much queries”? exists() is supposed to be the 
cheapest way of checking if the object exists. It seems like you want to check 
the database, without actually checking the database…

If you want to check a large number of objects at the same time, just do 
something like:

   InstanceModel.objects.filter(id__in=(your, list, of, PKs)).values_list('id', 
flat=True)

to get all pre-existing PKs in one query.

Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4848B682-ECB2-4DC9-83A7-5C7D58DE0EFF%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.