Neeraj Sharma
Neeraj Sharma

Reputation: 11

Django models naming convention

Can any one please look at the below piece of code to create table/class in models.py. I need these column names as I plan to auto upload the table values via excel on external location automatically. Hence dont want to change the column names. I have tried adding single quotes arround the column names with special characters.

Could you please help me create the below table/class. Currently it is indicating syntax error. I have got another class called Number for different app under the same project. Not sure if that is an issue.

from django.db import models


class Errors(models.Model):

    'S/O#' = models.IntegerField(max_length=10)

    Line Number = models.IntegerField()

    'S/O' Type = models.CharField(max_length=5)

    Error Detail = models.CharField(max_length=50)

    Comments = models.TextField()

    'Incident#' = models.CharField(max_length=10)

    Assigned To = models.CharField(max_length=10)

    Issue Status = models.CharField(max_length=15)

    Action = models.CharField(max_length=20)

Thanks

Upvotes: 0

Views: 1200

Answers (2)

Alexander Kononenko
Alexander Kononenko

Reputation: 2112

Just use "/" -> "_" and "#" -> "N".

Upvotes: 1

jpm
jpm

Reputation: 425

You won't be able to do that due to the restrictions of the python language.

You could perhaps look at overriding the default generated table and/or field name instead.

https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.Field.db_column https://docs.djangoproject.com/en/2.2/ref/models/options/#table-names

So instead of this:

'S/O#' = models.IntegerField(max_length=10)  # not a valid field name

Try something like this:

so_number = models.IntegerField(max_length=10, db_column='S/O#) 

Upvotes: 1

Related Questions