Django: Как проверить наличие дубликатов перед bulk_create (при определении объекта)?
Я хочу определить объект и проверить, является ли он дубликатом, прежде чем создавать его. дубликат перед его созданием, как показано ниже.
if data['title'] in videos:
Я надеюсь, что смогу определить это таким образом.
Как я могу определить дубликаты?
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)
Как вы сказали, если заголовок и thumbnail_url уникальны, сделайте это следующим образом:
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)