Mitch
Mitch

Reputation: 1634

Is it safe to make django field names camelCase?

I'm using django with an MSSQL Server backend. I ran inspectdb on a database and it made all my field names lowercase, when they were camelCase before.

I'd like to retain the camelCase (even if it doesn't jive with Python). I found where it does it in the inspectdb.py:

 """
        Modify the column name to make it Python-compatible as a field name
        """
        field_params = {}
        field_notes = []

        new_name = col_name.lower()
        if new_name != col_name:
            field_notes.append('Field name made lowercase.')

Is it safe to remove the col_name.lower(), or is there something that would cause the camelCase to cause issues?

Upvotes: 4

Views: 4154

Answers (3)

rosethorn
rosethorn

Reputation: 91

For my opinion, it's a coding style question. According the official document, it shows:

Field names should be all lowercase, using underscores instead of camelCase.

So, if you don't have any idea about that. Make sure you are following the document. It will be friendly to other developers.

Upvotes: 2

Ramez Ashraf
Ramez Ashraf

Reputation: 891

Nope it won't cause issues on the Django layer.

However, as you said, this is not pep8 style compatible and would lead to frustration along the way especially if other python developers got involved, and, as far as i know MSSQL is case insensitive to table/field names.

--Update-- @Wolph has a great suggestion

first_name = models.CharField(..., db_column='FirstName')

As to balance between python style and your legacy database.

Upvotes: 6

Wolph
Wolph

Reputation: 80031

Since MSSQL is case insensitive when it comes to column names, this won't make any difference to Django. Except for column name collisions that is, having both camelcase and camelCase is legal in Python but won't end well.

For your own convenience however, I would recommend to simply convert the camelcase fieldnames in the database to camel_case type naming within Python.

So if your table looks like this:

FirstName
LastName

The resulting Django model will have fields like this:

first_name = models.CharField(..., db_column='FirstName')
last_name = models.CharField(..., db_column='LastName')

Upvotes: 1

Related Questions