Advertisement
Guest User

Untitled

a guest
Dec 26th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. from django.core.management.base import BaseCommand
  2. from django.db import transaction
  3. from django.db.utils import IntegrityError
  4. from django.core.exceptions import ValidationError
  5. from rutube.management.commands.parser import parse
  6. from rutube.models import Video
  7.  
  8. class Command(BaseCommand):
  9.     help = 'Добавляет объект модели в таблицу'
  10.  
  11.     def handle(self, *args, **options):
  12.         videos_data = parse('https://rutube.ru/video/a40eadf2723be63c3a865e3790c1f52a/?playlist=329379')
  13.         for video_data in videos_data:
  14.             title = video_data['title']
  15.             description = video_data['description']
  16.             category = video_data['category']
  17.             img_link = video_data['img_link']
  18.             publication = video_data['publication']
  19.             duration = video_data['duration']
  20.             print('duration:', duration, type(duration))
  21.             video_link = video_data['video_url']
  22.        
  23.             obj, created = Video.objects.get_or_create(
  24.                 title=title, # это поле уникальное
  25.                 description=description,
  26.                 category=category,
  27.                 img_link=img_link,
  28.                 publication=publication,
  29.                 duration=duration,
  30.                 video_link=video_link # это поле уникальное
  31.                 )
  32.  
  33.             if created:
  34.                 print(f'New object added: {obj.title}')
  35.             else:
  36.                 print(f'Object with these values already exists: {obj.title}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement