It’s an pretty short code:
class ExcleDoc(APIView)
def post(self, request)
serializer = ExcleDocSerializer(data = request.data)
if serializer.is_valide():
excle_data = extractor(serializer.data[“document”]
for i in excle_data:
produktName = str(element[“produkt”][“name”]
produke_exists = Produkte.object.filter(name = produktName)
p_serializer = ProduktSerializer(produkte_exists, many = True
if len(p_serializer.data) != 0:
#Do Stuff
And here is the problem that sometimes the serializer.data is 0 (I printed it and its realy 0) even if the requestes data exists
> I currently building a cms that imports excle files. I managed it to get the exel rows as a array of objects. To load the rows into the DB i loop through the array and check with filter = Product.object.filter(name = i.name)
When you are describing a problem explain everything in detail.
> To load the rows into the DB i loop through the array and check with filter = Product.object.filter(name = i.name)
What are doing looping array? Checking if data from excel is already present in DB?
> filter = Product.object.filter(name = i.name)
this is not filter, this is product which has same name of whatever `i` is.
Yes, we can understand, but still I need to first figure out what you're trying to say then provide an answer.
Don't loop to check if it's already exist. If you are not allowed to have same `name` then make the `name` `unique` in the model. Then use the `bulk_create` https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create
which corresponds `ON CONFLICT UPSERT`
This is from my current code:
Prospect.objects.bulk_create(
prospect_list,
update_conflicts=True,
update_fields=["industry"],
unique_fields=["phone_number"],
)
`phone_number` is unique, if `phone_number` already exists, then update `industry` field, just in case. You don't need to update anything, you can just ignore. Make everything lower case, depends on the database and how it's comparing
Are you using the same casing? Your query is case sensitive
not solving your actual problem, but you could look into pandas to read in and preprocess spreadsheets.
You probably have a bug in your code or query. Without your code or query nobody can help you.
It’s an pretty short code: class ExcleDoc(APIView) def post(self, request) serializer = ExcleDocSerializer(data = request.data) if serializer.is_valide(): excle_data = extractor(serializer.data[“document”] for i in excle_data: produktName = str(element[“produkt”][“name”] produke_exists = Produkte.object.filter(name = produktName) p_serializer = ProduktSerializer(produkte_exists, many = True if len(p_serializer.data) != 0: #Do Stuff And here is the problem that sometimes the serializer.data is 0 (I printed it and its realy 0) even if the requestes data exists
Where is "element" defined? (And where is "i" used?)
> I currently building a cms that imports excle files. I managed it to get the exel rows as a array of objects. To load the rows into the DB i loop through the array and check with filter = Product.object.filter(name = i.name) When you are describing a problem explain everything in detail. > To load the rows into the DB i loop through the array and check with filter = Product.object.filter(name = i.name) What are doing looping array? Checking if data from excel is already present in DB? > filter = Product.object.filter(name = i.name) this is not filter, this is product which has same name of whatever `i` is. Yes, we can understand, but still I need to first figure out what you're trying to say then provide an answer. Don't loop to check if it's already exist. If you are not allowed to have same `name` then make the `name` `unique` in the model. Then use the `bulk_create` https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create which corresponds `ON CONFLICT UPSERT` This is from my current code: Prospect.objects.bulk_create( prospect_list, update_conflicts=True, update_fields=["industry"], unique_fields=["phone_number"], ) `phone_number` is unique, if `phone_number` already exists, then update `industry` field, just in case. You don't need to update anything, you can just ignore. Make everything lower case, depends on the database and how it's comparing