Reputation: 495
I would like to define an object and check if it is a duplicate before creating it,as shown below.
if data['title'] in videos:
I'm hoping I can determine it this way.
How can I determine duplicates?
videos = []
throughs = []
for datas in files:
for data in datas:
tags = data["tags"].split()
tag_pks = list(
set([Tag.objects.get_or_create(name=tag)[0].pk for tag in tags])
)
#Around here, we want to make sure that the list called videos already contains data['title'].
video = Video(
title=data["title"],
thumbnail_url=data["thumbnail_url"],
preview_url=data["preview_url"],
embed_url=data["embed_url"],
embed_source=data["embed_source"],
duration=data["duration"],
published_at=data["published_at"],
)
for tag_pk in tag_pks:
throughs.append(
Video.tags.through(video_id=video.pk, tag_id=tag_pk)
)
videos.append(video)
Video.objects.bulk_create(videos)
Video.tags.through.objects.bulk_create(throughs)
Upvotes: 0
Views: 236
Reputation: 324
Like you said if the title and the thumbnail_url are unique, do it like this:
videos = []
throughs = []
for datas in files:
for data in datas:
tags = data["tags"].split()
tag_pks = list(
set([Tag.objects.get_or_create(name=tag)[0].pk for tag in tags])
)
#Around here, we want to make sure that the list called videos already contains data['title'].
video = Video(
title=data["title"],
thumbnail_url=data["thumbnail_url"],
preview_url=data["preview_url"],
embed_url=data["embed_url"],
embed_source=data["embed_source"],
duration=data["duration"],
published_at=data["published_at"],
)
for tag_pk in tag_pks:
throughs.append(
Video.tags.through(video_id=video.pk, tag_id=tag_pk)
)
# before you put it in the list check if this obj is in the list already
# since it is empty for the first loop we will jump to the else and add the first one
if videos:
# then loop that list
for vid in videos:
# and check if the new obj is in that list
if vid.title != video.title and vid.thumbnail_url != video.thumbnail_url:
# if the condition is true that means it is not in the list so we can add
# otherwise we will skip
videos.append(video)
else:
videos.append(video)
Video.objects.bulk_create(videos)
Video.tags.through.objects.bulk_create(throughs)
Upvotes: 1