简体   繁体   中英

python django name conventions

the naming convention for ForeignKey is to name with the lowercase version of the connected model , the following is taken from the docs:

class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer)
    # ...

class Manufacturer(models.Model):
    # ...
    pass

but I have the following models:

class Work(models.model):
    class = models.ForeignKey(Class)
    #...

class Class(models.model):
    #...
    pass

As we know, this will raise an error because we cannot set a variable to class because it is built into python.

Question:

will not following the naming conventions actually be a problem in the SQL database creation?

for example:

class Work(models.model):
    cls = models.ForeignKey(Class)
    #...

class Class(models.model):
    #...
    pass

will having a different ForeignKey name mess up the SQL table and database creation?

You definitely want to stay away from reserved words such as Class, List, String, etc. Whether it will mess up your database or not, I'm not sure (try it and see!) but it's definitely a bad idea. If it's not a sql problem, it will mess up something somewhere eventually.

I see 'klass' a lot, if you feel like the word class is absolutely necessary. 'Course', perhaps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM