Error creating a simple Django table?

Asked

Viewed 114 times

1

<code>Erro Apresentado</code>

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 he report identation error but I’m blind I’m beginner in language so any obvious mistake please speak to me.

  • Please note that in the option Ordering of the Meta class you use 'product name', This is different from the compo product name that you defined in the Product class, that way you are not referencing the field, because product name is different from product name.

2 answers

2


I don’t know which tool you’re using to write your code, but most of them have a mechanism that helps you keep indenting correctly.

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

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

If you create a method within the class, the code of this method needs to have 8 spaces.

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

I recommend reading the style guide written by python Brazil.

0

This must 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 may 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 type of data, thus remaining:

db_table = 'usuarios'

https://docs.djangoproject.com/en/2.0/ref/models/options/#db-table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.