Skip to content

Commit

Permalink
1 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MachineLearningIsEasy committed Mar 29, 2020
1 parent 1b34f79 commit 31d2302
Show file tree
Hide file tree
Showing 69 changed files with 13,961 additions and 0 deletions.
Empty file.
6 changes: 6 additions & 0 deletions django_test/articles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

from .models import Tag, Article

admin.site.register(Tag)
admin.site.register(Article)
5 changes: 5 additions & 0 deletions django_test/articles/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ArticlesConfig(AppConfig):
name = 'articles'
20 changes: 20 additions & 0 deletions django_test/articles/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django import forms
from .models import Tag
from .models import Article

'''
class ArticleForm(forms.Form):
name=forms.CharField(label = 'Название статьи', max_length = 100)
text=forms.CharField(label = 'Текст статьи')
tags=forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
widget=forms.CheckboxSelectMultiple())
'''

class ArticleForm(forms.ModelForm):
article_name = forms.CharField(label = 'Название статьи')

class Meta:
model = Article
fields = '__all__'
# fields = ('article_name', 'article_text')
# exclude = ('tags')
Empty file.
Empty file.
43 changes: 43 additions & 0 deletions django_test/articles/management/commands/db_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.core.management.base import BaseCommand
from articles.models import Article, Tag


class Command(BaseCommand):

def handle(self, *args, **options):



# Запрос в базу без условия!
print('DB command!')
articles = Article.objects.all()
print(type(articles), articles)
for art in articles:
print(f'Name: {art.article_name}, Text: {art.article_text}, Tags: {art.tag_number()}')

#Запрос в базу c условием!
#
# # get
# art_ = Article.objects.get(article_name = 'Happy lion')
# print(art_)
# # filter
# tags = Tag.objects.filter(tag_name = 'nature')
# print(tags)
#
# # Связанные поля
# print(art_.article_tag.all())
# print(art_.article_tag.first())
#
#
# # Добавление в БД
# Tag.objects.create(tag_name = 'Travelling')
# tags = Tag.objects.all()
# print(tags)
#
# # Удаление
# tag = Tag.objects.filter(tag_name='Travelling')
# tag.delete()


tags = Tag.objects.all()
print(tags)
8 changes: 8 additions & 0 deletions django_test/articles/management/commands/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.management.base import BaseCommand


class Command(BaseCommand):

def handle(self, *args, **options):

print('Hello!')
33 changes: 33 additions & 0 deletions django_test/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.0.3 on 2020-03-17 09:11

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Tag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tag_name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('article_name', models.CharField(max_length=100, null=True)),
('article_text', models.CharField(max_length=1000, null=True)),
('article_date', models.DateTimeField(default=datetime.datetime(2020, 3, 17, 9, 11, 35, 433347))),
('article_img', models.ImageField(blank=True, null=True, upload_to='articles')),
('article_tag', models.ManyToManyField(to='articles.Tag')),
],
),
]
Empty file.
59 changes: 59 additions & 0 deletions django_test/articles/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.db import models
from datetime import datetime
import os
from django.conf import settings

# Create your models here.

#https://djbook.ru/rel1.9/topics/db/models.html#model-inheritance - наследование моделей

class Tag(models.Model, ):
tag_name = models.CharField(max_length = 100)

def __str__(self):
return f'{self.tag_name}'

class Article(models.Model):
article_name = models.CharField(max_length = 100, null = True)
article_text = models.CharField(max_length = 1000, null = True)
article_date = models.DateTimeField(default = datetime.now())
article_tag = models.ManyToManyField(Tag)
article_img = models.ImageField(upload_to = 'articles', null = True, blank = True)
#, default = os.path.join(settings.MEDIA_ROOT,'articles','happy_lion.jpg')

def tag_number(self):
return len(self.article_tag.all())


def is_tag_one(self):
if len(self.article_tag.all()) == 1:
return True
return False
'''
DataField - дата
TimeField - время
Числовые типы:
IntegerField
PositeveIntegerField
FloatField
Логические типы:
BooleanField
Байтовый тип:
BinaryField
Email:
EmailField
URL:
URLField
Image:
ImageField
'''

def __str__(self):
return f'{self.article_name}, published: {self.article_date}'

2 changes: 2 additions & 0 deletions django_test/articles/static/articles/aos.css

Large diffs are not rendered by default.

Loading

0 comments on commit 31d2302

Please sign in to comment.