How to bulk_create in DRF Serializer to seperate django instances
I get an array of urls and the goal is to write each url as separate model instance with other constant params that are same for all urls
django model:
class BlockedUrl(models.Model):
url = models.URLField()
date_add = models.DateField(auto_now_add=True)
class Meta:
app_label = 'main'
db_table = 'blocked_url'
ordering = ['-date_add']
django view:
from main.models import BlockedUrl
from api.serializers import BlockedUrlSerializer
class BlockedUrlsViewSet(GenericViewSet, CreateModelMixin):
""" обновление несобранных (заблокированных) ссылок
"""
queryset = BlockedUrl.objects.all()
serializer_class = BlockedUrlSerializer
permission_classes = (AllowAny,)
django serializer:
class BlockedUrlSerializer(Serializer):
urls = ListField(child=URLField(), min_length=1, max_length=1024)
# create - to do (iter the list of urls and insert separately into model with one transaction)
def create(self, validated_data):
crawler = validated_data['crawler']
blocked_urls = [BlockedUrl(url=url) for url in validated_data['urls']]
return BlockedUrl.objects.bulk_create(blocked_urls)
# update - raise not implemented error
def update(self, instance, validated_data):
raise NotImplementedError
but it does not work as I get AttributeError: 'list' object has no attribute 'urls'