Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home work #310

Open
Gitfixing opened this issue Jul 24, 2023 · 0 comments
Open

Home work #310

Gitfixing opened this issue Jul 24, 2023 · 0 comments

Comments

@Gitfixing
Copy link

Для изменения имени таблицы необходимо в модели добавить метакласс с атрибутом db_table, указывающим новое имя таблицы. Например:

class Advertisement(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)

class Meta:
    db_table = 'advertisements'

Затем необходимо создать миграцию командой python manage.py makemigrations и применить ее командой python manage.py migrate.

Для переопределения строки вывода объекта модели необходимо в классе модели определить метод str, который будет возвращать нужную строку. Например:

class Advertisement(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)

def __str__(self):
    return f"Advertisement(id={self.id}, title={self.title}, price={self.price})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant