Re: Fast and Easy way to check if an instance of an Model exists in the Database
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
> <johannes.schneider@galileo-press.de> 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.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/52DCDA7B.4000206%40galileo-press.de.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home