0

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

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • I don't think that has anything to do with Django. Please refer to Python's documentation to find out what characters are acceptable as names – Pynchia Aug 21 '19 at 03:27
  • Possible duplicate of [Valid characters in a python class name](https://stackoverflow.com/questions/10120295/valid-characters-in-a-python-class-name) – Pynchia Aug 21 '19 at 03:28
  • Thanks for the reply Pynchia. I have tried single and double quotes which was suggested for other special characters. Secondly for testing purpose I have added underscore to isolate acceptable names but no luck. – Neeraj Sharma Aug 21 '19 at 03:52

2 Answers2

1

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#) 
jpm
  • 405
  • 5
  • 7
  • Thanks buddy. db_column attribute has cleared the error. I have now used it for all the column names with special characters and not allowed names. I will now work on my auto upload. – Neeraj Sharma Aug 21 '19 at 07:30
1

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

Alexander Kononenko
  • 1,952
  • 1
  • 10
  • 17