Database operators for custom fields

67 views
Skip to first unread message

Johannes Schneider

unread,
Feb 19, 2014, 6:45:43 AM2/19/14
to django...@googlegroups.com
hey list,

I have a custom Field implemented for one of my models. How do I
implement the database operations (e.g. '==', '!=') for the custom field
which are used to execute database queries?

bg,
Johannes
--
Johannes Schneider
Webentwicklung
johannes....@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

Serdar Dalgic

unread,
Feb 19, 2014, 6:58:53 AM2/19/14
to django...@googlegroups.com
Hi!

In python, '==' operator is set to '__eq__' and '!=' operator is set
to '__ne__' functions.

See more details in http://jcalderone.livejournal.com/32837.html

And In order to make queries from the ORM, Django uses Managers
https://docs.djangoproject.com/en/dev/topics/db/managers/

So you can write a Manager for the model that has this custom field,
override the __eq__ and __ne__ operations on this custom field and
implement as you like.

Good days!
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/53049967.8030907%40galileo-press.de.
> For more options, visit https://groups.google.com/groups/opt_out.



--
- Serdar Dalgıç <s...@serdardalgic.org>
FLOSS Developer, Life & Nature Hacker
twitter:
https://twitter.com/serdaroncode
https://twitter.com/serdarintowild

Anssi Kääriäinen

unread,
Feb 19, 2014, 5:19:48 PM2/19/14
to django...@googlegroups.com
If I am not mistaken you are trying to alter how .filter(customfield__eq=val) and friends work.

In current Django versions you can't easily customize the SQL generated for ORM lookups. In Django 1.7 (currently in alpha) this is possible using custom lookups. Custom lookups will allow you to alter the SQL generated for different lookups (and also add new lookups if need be). See https://docs.djangoproject.com/en/dev/ref/models/custom-lookups/ for all the details.

 - Anssi

Johannes Schneider

unread,
Feb 20, 2014, 2:59:41 AM2/20/14
to django...@googlegroups.com
thnx, yea that's what I'm looking for. unfortunately I cannot use 1.7 ;(

bg,
Johannes
On 19.02.2014 23:19, Anssi Kääriäinen wrote:
> If I am not mistaken you are trying to alter how
> .filter(customfield__eq=val) and friends work.
>
> In current Django versions you can't easily customize the SQL generated
> for ORM lookups. In Django 1.7 (currently in alpha) this is possible
> using custom lookups. Custom lookups will allow you to alter the SQL
> generated for different lookups (and also add new lookups if need be).
> See https://docs.djangoproject.com/en/dev/ref/models/custom-lookups/ for
> all the details.
>
> - Anssi
>
> On Wednesday, February 19, 2014 1:45:43 PM UTC+2, johannes.schneider wrote:
>
> hey list,
>
> I have a custom Field implemented for one of my models. How do I
> implement the database operations (e.g. '==', '!=') for the custom
> field
> which are used to execute database queries?
>
> bg,
> Johannes
> --
> Johannes Schneider
> Webentwicklung
> johannes....@galileo-press.de <javascript:>
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/9edddaa4-0c5a-429d-8743-1b8ca064dfae%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.


Reply all
Reply to author
Forward
0 new messages