views:

257

answers:

3

In Django if I an object of type: django.forms.BooleanField for example how do I know what database type it is going to be saved to e.g. Int, Boolean, Varchar? I know Django automatically handles this as part of the models but I want to do this manually so that is not an option. Is there a built in Django function I can call which will tell me this information or would I have to do it manually for example create a function which returns the db type for a passed in form element?

A: 

A BooleanField will mostly be saved as a boolean, except for when using a MySQL backed Django project:

A boolean field in MySQL is stored as a TINYINT column with a value of either 0 or 1 (most databases have a proper BOOLEAN type instead).

See the docs.

There's no built in way to override the field types as far as I know (it's not listed in the docs). Why would you want to do that? Are you trying to insert data into an already existing table? If you want to do this, create the table yourself manually, and use the managed Meta option.

Dominic Rodger
I'm not trying to override it I just want to find out what a field maps to in a database. So using the example if I had a field of type BooleanField I would know that this is going to be stored as a TINYINT.Some sudo code might be something like:frmObj = django.forms.BooleanFielddbType = db_type(django.forms.BooleanField)print dbType # this prints out TINYINT or another django model which is equal to TINYINT
John
But why? With the exception of MySQL `BooleanField`s mapping to `TINYINT`, aren't all the field types fairly obvious? Why would you need to know how they're stored? One of the great things about Django is the fact that that stuff (how exactly data is stored) is abstracted away for you.
Dominic Rodger
Yeah they are all fairly obvious and I don't think it would be too difficult to write a function to do what I'm after but it just seemed like something that would be already written. If you read the above posts (saves me re-typing :)) I'm trying to store data returned from a dynamic form. But rather than saving all the data as simply varchars I would like to store what type of data they should be e.g. dates, ints, booleans etc. which I could then use for future processing
John
A: 

That question doesn't really make sense. A form, as opposed to a modelform, doesn't have any connection with a database, so there isn't any such 'mapping', and and you can't 'save' it.

Edited in response to comment: Fair enough, but there simply isn't any mapping as you describe. If you want to save the result of a non-model form to a database, you will need to define how that gets done. So it will be up to you to decide what type to save your field as.

Second edit no, because there's no relationship between forms and the database. I don't know how I can explain this any further. A model field knows a) what database types to create and b) what form fields to create, but that mapping doesn't work in reverse - why would it? If you want to save a form field to the database, you need to know more than just 'what data type is it'. You'd need to know the name of the column to save it in, the table that column belongs in, and any constraints that the column needs to fulfil - none of which are defined by a form, as they are all defined by the model.

You could try and iterate through all the different model field types in django.db.models.fields, look at the formfield() method of each one to see if it creates a BooleanField, then if it does get the database type from get_internal_type(), but this strikes me as being a totally horrible, ugly and inefficient thing to want to do - not to mention the fact that there's no reason why two model fields mapping to different database types couldn't both use a BooleanField as their form field representation.

Daniel Roseman
I have a form which is dynamically built on the fly. This form might consist of:django.forms.CharField,django.forms.MultipleChoiceField,django.forms.IntegerFielddjango.forms.DateFieldI now what to save this to a database. I have a table set up with the following structure:id | answer | answer Typeid is an auto increment field. answer will be the value returned by the form. This column is of type varchar. answer_type will return the type of data returned. So from the example form above the first value is varchar, the next varchar, next int and finally datetime
John
See my extended reply above.
Daniel Roseman
Surely there must be something in Django that says: If the model has a field of type django.models.DateTimeField then create a form element of type django.forms.DateField as this is how modelforms works. So in reverse is there not a function which says if a object is of type: django.forms.DateField then the field object must be of type: django.models.DateTimeField Which would then in turn tell you that objects of type: django.models.DateTimeField are stored as db type datetime
John
Updated again...
Daniel Roseman
A: 

I know this thread is old by now, but I wanted to post that there's already a way to determine what tables/fields are being created with django:

django-admin.py sqlall

see the django-admin docs for more details.

Erin Heyming