Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.core.management.base import BaseCommand
- from django.db import transaction
- from django.db.utils import IntegrityError
- from django.core.exceptions import ValidationError
- from rutube.management.commands.parser import parse
- from rutube.models import Video
- class Command(BaseCommand):
- help = 'Добавляет объект модели в таблицу'
- def handle(self, *args, **options):
- videos_data = parse('https://rutube.ru/video/a40eadf2723be63c3a865e3790c1f52a/?playlist=329379')
- for video_data in videos_data:
- title = video_data['title']
- description = video_data['description']
- category = video_data['category']
- img_link = video_data['img_link']
- publication = video_data['publication']
- duration = video_data['duration']
- print('duration:', duration, type(duration))
- video_link = video_data['video_url']
- obj, created = Video.objects.get_or_create(
- title=title, # это поле уникальное
- description=description,
- category=category,
- img_link=img_link,
- publication=publication,
- duration=duration,
- video_link=video_link # это поле уникальное
- )
- if created:
- print(f'New object added: {obj.title}')
- else:
- print(f'Object with these values already exists: {obj.title}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement