On Nov 26, 9:06 pm, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Wed, 2008-11-26 at 19:50 -0800, nbv4 wrote: > > I'm new to django, and I'm having a problem setting up a model. > > > I have a model which has about 12 decimal fields and two integer > > fields. Each field, when displayed, will always be blank if the value > > is zero. In other words, "0" or "0.0" will never be displayed, either > > when the object is outputted to the page, or in a form derived from > > that model. Whats the best way to set up each field, in regards to > > null=True/False, blank=True/False default=0/Null, etc. > > > I want to be able to create a form derived from this model, and I want > > all the fields in that form to start out as blank, so I guess I want > > "default=Null", but doing this creates a NULL in the database, which > > screws up numerical calculations. > > Since "blank" isn't a valid concept for a numerical field, what you need > to do is write your own form field class that knows how to convert > between "0" and blank for rendering purposes (it will need to use a text > widget of some kind for the rendering) and back for validation. You > won't be able to achieve this affect automatically, since it doesn't > normally make sense. What happens if somebody deliberately enters "0" > into a field for example? When you redisplay the form, to correct some > other error, their input has been lost. It will make it hard to tell the > difference between fields with required values and a zero entered in > them and field with required values that haven't been filled in yet. > > As for the specifics, if you really want to do this, have a read of > django/forms/fields.py to see how Field classes are implemented and > django/forms/widgets.py for the corresponding widgets. That will give > you some ideas for how to do this. > > Regards, > Malcolm
For the most part, the each instance of this model will have one or maybe two fields a non-zero value, while the rest are zero. When these objects are displayed, they'll be in a table, and it will just look better to depict zero's as blank cells, rather than having a bunch of zero's all over the place. The reason I want blanks on the form, is because I want the user to be able to just enter the value right into the box without having to first delete the zero, then type their value in. The more I think about it, the more I think it would be best to handle turning all the zero's into blanks at the template level, or even post template via javascript. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---