Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-16 Thread rmschne
To complete the story, in case someone finds this in the future, I
ended up with the following which at the time of this writing seems to
work:

class MyNullBooleanField(models.NullBooleanField):
"""Designed to work with how Microsoft Access stores booleans as
-1 and 0 into MySQL.
The to_python function was taken from the Django modeld with
addition of -1 for true."""
__metaclass__ = models.SubfieldBase
def to_python(self,value):
if value in (True, False):
return bool(value)
if value in ('None',):
return None
if value in ('t', 'True', '1', '-1', 1, -1):
return True
if value in ('f', 'False', '0', 0):
return False
if value is None:
return None

The whole to_python function was taken from the standard definition
from the Django library.  My simple first draft wasn't complete
enough. I found that i needed to test for the integers 0,1, and -1 in
addition to their string equivalents in if value in "('t', 'True',
'1', '-1', 1, -1)" and "if value in ('f', 'False', '0', 0)". With that
change it appears to handle the boolean data stored into MySQL as
written by the Microsoft Access forms on the front end.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-16 Thread rmschne
(While slapping my forehead) ... figured it out.  Have to remove
"models" as a prefix to MyNullBooleanField, as of course, the field
definition is not in models.

forinvoice.MyNullBooleanField(null=True,
db_column='forinvoice', blank=True)

Now to get on debugging the functionality of the new field.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-16 Thread rmschne
Tom,

Thanks ... Can I get a bit more of your brain power. I'm struggling
with understanding the custom-model-fields document sufficient to
allow me to do it.

At the top of the models.py files, I put in:

class MyNullBooleanField(models.NullBooleanField):
"""Designed to work with how Microsoft Access stores booleans as
-1 and 0 into MySQL"""
__metaclass__ = models.SubfieldBase
def to_python(self,value):
if value is None: return
if value == 0:
return False
else:
return True

down further in that file, as a first test of this new field, for the
table/class where I want to use it:

class MemberAddress(models.Model):
member=models.ForeignKey(Member,related_name='addresses',
db_column='memberid')
city = models.CharField(max_length=50, db_column='city',
blank=True)
forinvoice=models.MyNullBooleanField(null=True,
db_column='forinvoice', blank=True)

When I run the code I get the error on the above "forinvoice" line:

AttributeError: 'module' object has no attribute 'MyNullBooleanField'

I was assuming that by defining the new custom field at the top of the
models.py file, it would work.  I can't find anything in the custom-
model-fields doc which says "where" to put this custom definition.  I
just assumed they intended it to go into models.py

What am i doing wrong?  Thanks!!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-15 Thread Tom Evans
On Thu, Jul 15, 2010 at 7:47 AM, rmschne  wrote:
> Oh ... by the way, we aren't using Access as a front end to Django.
> There is nothing (far as I know) in Django to front-end to!
>
> This app has been successfully making us money for more than 20
> years.  The data side moved to MySQL a long time ago (can't remember
> when) to enhance performance and security, but the relatively
> sophisticated Access side remained in place and continued to evolve.
> Still see no viable replacement on the horizon for Access for the
> front end for use by people.  There are a number of tools that have
> suficient capability to replace it but all would cost a fortune to
> make it happen.  Instead, we're using Python/Django as a basis now for
> enhanced reporting/number-crunching and future automation (which sends
> us in the direction of having so many people having to use the Access
> app).   Had we had Python and Django in the late 1980's we'd probably
> be there now.
>

The thing I was trying to get across is that the model definitions is
basically a contract of what kind of data is stored in the database,
and every time you modify the data directly and not through the ORM,
you must take extra special care not to break the contract.

In your case, you actually have two contracts, one with your legacy
application, and one with django, and your django contract contradicts
your legacy one, so you had these problems.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-15 Thread rmschne
Oh ... by the way, we aren't using Access as a front end to Django.
There is nothing (far as I know) in Django to front-end to!

This app has been successfully making us money for more than 20
years.  The data side moved to MySQL a long time ago (can't remember
when) to enhance performance and security, but the relatively
sophisticated Access side remained in place and continued to evolve.
Still see no viable replacement on the horizon for Access for the
front end for use by people.  There are a number of tools that have
suficient capability to replace it but all would cost a fortune to
make it happen.  Instead, we're using Python/Django as a basis now for
enhanced reporting/number-crunching and future automation (which sends
us in the direction of having so many people having to use the Access
app).   Had we had Python and Django in the late 1980's we'd probably
be there now.

On Jul 14, 3:57 pm, Tom Evans  wrote:
> On Wed, Jul 14, 2010 at 1:45 PM, rmschne  wrote:
> > As I understand, in Django/Python, True is 1 and False=0.  And when
> > connected to the database, we use a TinyInt for that variable and
> > assign it to a NullBooleanField.
>
> True and False are global objects of type bool, not 1 and 0. bool
> constructor converts integers to True/False as appropriate.
>
>
>
> > Problem is that some people use their PC's with a Microsoft Access
> > based front end to the database (MySQL).  The forms use check-boxes,
> > and when checked, which is supposed be "true", Access puts -1 into the
> > data base.  Django doesn't recognize that value as True.
>
> Yes, Access is dire. I think you can probably count the number of
> people using Access as a front end to django on one hand (possibly one
> hand with four fingers cut off).
>
>
>
> > I can't change the Access forms or system and don't tell me to stop
> > using Access. We don't have unlimited resources to fix all the
> > problems in the world!
>
> > I'm wondering if there is some way to tell Django in the data model to
> > let a model variable return True when <>0 (instead of when=1) , and
> > False when 0?
>
> > This seems the cleanest easiest way; but I can't see how to make this
> > possible?  Is it?  Or is there another approach ?
>
> Define a MyBooleanField that extends models.BooleanField, override
> to_python(). Use that instead of models.BooleanField.
>
> Docs on that:http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/
>
> Cheers
>
> Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-14 Thread rmschne
Tom,

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-14 Thread Tom Evans
On Wed, Jul 14, 2010 at 1:45 PM, rmschne  wrote:
> As I understand, in Django/Python, True is 1 and False=0.  And when
> connected to the database, we use a TinyInt for that variable and
> assign it to a NullBooleanField.

True and False are global objects of type bool, not 1 and 0. bool
constructor converts integers to True/False as appropriate.

>
> Problem is that some people use their PC's with a Microsoft Access
> based front end to the database (MySQL).  The forms use check-boxes,
> and when checked, which is supposed be "true", Access puts -1 into the
> data base.  Django doesn't recognize that value as True.

Yes, Access is dire. I think you can probably count the number of
people using Access as a front end to django on one hand (possibly one
hand with four fingers cut off).

>
> I can't change the Access forms or system and don't tell me to stop
> using Access. We don't have unlimited resources to fix all the
> problems in the world!
>
> I'm wondering if there is some way to tell Django in the data model to
> let a model variable return True when <>0 (instead of when=1) , and
> False when 0?
>
> This seems the cleanest easiest way; but I can't see how to make this
> possible?  Is it?  Or is there another approach ?
>

Define a MyBooleanField that extends models.BooleanField, override
to_python(). Use that instead of models.BooleanField.

Docs on that:
http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Redefine True and False for Boolean and/or NullBoolean Fields?

2010-07-14 Thread rmschne
As I understand, in Django/Python, True is 1 and False=0.  And when
connected to the database, we use a TinyInt for that variable and
assign it to a NullBooleanField.

Problem is that some people use their PC's with a Microsoft Access
based front end to the database (MySQL).  The forms use check-boxes,
and when checked, which is supposed be "true", Access puts -1 into the
data base.  Django doesn't recognize that value as True.

I can't change the Access forms or system and don't tell me to stop
using Access. We don't have unlimited resources to fix all the
problems in the world!

I'm wondering if there is some way to tell Django in the data model to
let a model variable return True when <>0 (instead of when=1) , and
False when 0?

This seems the cleanest easiest way; but I can't see how to make this
possible?  Is it?  Or is there another approach ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.