from django.db import models
class Bb(models.Model):
title = models.CharField(max_length=50)
content = models.TextField(null=True, blank=True)
price = models.FloatField(null=True, blank=True)
published = models.DateTimeField(auto_now_add=True, db_index=True)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
manage.py makemigrations btest
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Bb',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('content', models.TextField(blank=True, null=True)),
('price', models.FloatField(blank=True, null=True)),
('published', models.DateTimeField(auto_now_add=True, db_index=True)),
],
),
]
manage.py sqlmigrate btest 0001
manage.py migrate
manage.py shell
from btest.models import Bb
b1 = Bb(title='Дом', content='ООО "Агент". Два этажа, кирпич, свет, газ, канализация', price=10000000)
b1.save()
b1.pk
#Выведет: 1
b1.title
#Выведет: 'Дом'
Bb.objects.create(title='Дача', content='Один этаж. Брус.', price=100000000)
exit()
from django.http import HttpResponse
from .models import Bb
def index(request):
s = 'Список объявлений\r\n\r\n\r\n'
for bb in Bb.objects.order_by('-published'):
s += bb.title + '\r\n' + bb.content + '\r\n\r\n'
return HttpResponse(s, content_type='text/plain; charset=utf-8')