Can I name the "id" field in a Django model?

class MyModel(models.Model):
    id = models.IntegerField(primary_key=True)

or

class MyModel(models.Model):
    id_ = models.IntegerField(primary_key=True)

According to pep8, single_trailing_underscore_ should be used to avoid conflicts with the Python keyword, but a column named id_ would look ugly and possibly lead to confusion with someone not familiar with python at the database level.

Django docs uses the id column name: https://docs.djangoproject.com/en/1.11/ref/models/fields/#uuidfield .

Can this field be called "id"?

+6
source share
2 answers

Python . LEGB ( , , , ). ; id - . . "" , . :

  • id() .
  • , , , .
  • .id Django, Django , .
+5

, id, Django, , . Django id,

id = models.AutoField(primary_key=True)

+5

Source: https://habr.com/ru/post/1017030/


All Articles