Error when creating a Django simple table?

1

class Produtos(models.Model):
    #id = models.AutoField(primary_key=True)
    nomeproduto = models.CharField(max_length=50, blank=True, null=True)
    quantproduto = models.CharField(max_length=30, blank=False, unique=True)
status = models.CharField('Status', max_length=10, default='ATIVO', blank=False, null=False)

    class Meta:
        db_table: str = 'usuarios'
        ordering = ('nome_produto',)

The main problem is it inform indentation error but I am blind I am beginner in the language so any obvious error tell me, please.

    
asked by anonymous 22.06.2018 / 15:43

2 answers

2

I do not know which tool you're using to write your code, but most of them have a mechanism that helps maintain the indentation correctly.

For python, there are 4 spaces for each level of indentation.

Then, when defining the class, without indentation. For each attribute, field, and etc within that class, you need to add 4 spaces.

If you create a method within the class, then the code for that method needs to have 8 spaces.

With your condition, I recommend caution when copying codes from here or elsewhere, as they may contain extra spaces and cause errors like this.

I recommend reading the style guide written by python Brazil .

    
22.06.2018 / 17:22
0
  

This should be the correct indentation

class Produtos(models.Model):
    #id = models.AutoField(primary_key=True)
    nomeproduto = models.CharField(max_length=50, blank=True, null=True)
    quantproduto = models.CharField(max_length=30, blank=False, unique=True)
    status = models.CharField('Status', max_length=10, default='ATIVO', blank=False, null=False)

Another point that might cause an error is how you declared the table name in the Meta class. According to the django documentation it is not necessary to specify the data type, thus:

db_table = 'usuarios'

link

    
23.06.2018 / 16:53