Thanks for pointing me in the right direction.  Here's the solution I
came up with:

import socket, struct

class ipfield(models.Field):
        __metaclass__ = models.SubfieldBase
        def db_type(self):
                return 'integer'

        def to_python(self, value):
                return socket.inet_ntoa(struct.pack('!L', value))


class tbl(models.Model)
       ip_field = ipfield()

Note that this only deals with pulling data from the database, I don't
need to worry about putting data in.

Thanks!
-Sned

On Dec 1, 11:44 am, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Dec 1, 6:48 pm, sned <[EMAIL PROTECTED]> wrote:
>
>
>
> > Is there a way to tell django to use a specific function when
> > selecting data from a model?
>
> > The sql is relatively simple, but I don't know how to convert that to
> > model syntax.
>
> > Here's the issue:  a table I'm working with stores ip addresses as a
> > 10 digit integer.  To get the ip address out of that, the sql is
> > simply SELECT inet_ntoa(ip_field) FROM tbl;
>
> > I'm assuming there's some way to tell django to do that cast in the
> > model:
>
> > class tbl(models.Model)
> >     ip_field = models.IntegerField(  what do i do here? )
>
> > Anyone have an idea?
>
> > Thanks!
> > -sned
>
> AFAIK you can't easily define custom SQL to be used when getting a
> field from the database. One approach might be to create a custom
> model field[1], and override the to_python method to do the inet_ntoa
> call in Python[2] (and similarly use the db_prep_save method to
> convert back using inet_aton).
>
> [1] Full instructions for custom model 
> fields:http://docs.djangoproject.com/en/dev/howto/custom-model-fields/
>
> [2] Python's socket module actually converts to and from packed
> strings, you need struct.pack/unpack to get to and from integers. See
> a recipe here:http://code.activestate.com/recipes/66517/
>
> --
> DR
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to