Articles
Uppdatera modell Django
On februari 18, 2021 by adminHur kan jag förbättra detta? Det är stort och ful. Några råd?
Jag får en POST från ett API och jag vill bara uppdatera de fält som inte är null.
titulo = request.data.get("titulo", "") image = request.data.get("image", "") price = request.data.get("price", "") wholesale_price = request.data.get("wholesale_price", "") reference = request.data.get("reference", "") ean13 = request.data.get("ean13", "") rating = request.data.get("rating", "") sales = request.data.get("sales", "") active = request.data.get("active", "") encilleria = request.data.get("encilleria", "") delivery = request.data.get("delivery", "") summary = request.data.get("summary", "") brand_id = request.data.get("brand_id", "") consejos = request.data.get("consejos", "") ingredientes = request.data.get("ingredientes", "") stock = request.data.get("stock", "") es_pack = request.data.get("es_pack", "") descontinuado = request.data.get("descontinuado", "") tags = request.data.get("tags", "") destacado_buscado = request.data.get("destacado_buscado", "") supplier_delivery = request.data.get("supplier_delivery", "") imagen_mala = request.data.get("imagen_mala", "") guia_cosmetica_attrs = request.data.get("guia_cosmetica_attrs", "") guia_cosmetica_cats = request.data.get("guia_cosmetica_cats", "") try: producto = Product.objects.get(pk=product_id) if titulo != "": producto.titulo = titulo if titulo != "": producto.slug = slugify(titulo) if image != "": producto.image = image if price != "": producto.price = price if wholesale_price != "": producto.wholesale_price = wholesale_price if reference != "": producto.reference = reference if ean13 != "": producto.ean13 = ean13 if rating != "": producto.rating = rating if sales != "": producto.sales = sales if active != "": producto.active = active if encilleria != "": producto.encilleria = encilleria if delivery != "": producto.delivery = delivery if summary != "": producto.summary = summary if brand_id != "": producto.brand_id = brand_id if consejos != "": producto.consejos = consejos if ingredientes != "": producto.ingredientes = ingredientes if stock != "": producto.stock = stock if es_pack != "": producto.es_pack = es_pack if descontinuado != "": producto.descontinuado = descontinuado if tags != "": producto.tags = tags if destacado_buscado != "": producto.destacado_buscado = destacado_buscado if supplier_delivery != "": producto.supplier_delivery = supplier_delivery if imagen_mala != "": producto.imagen_mala = imagen_mala if guia_cosmetica_attrs != "": producto.guia_cosmetica_attrs = guia_cosmetica_attrs if guia_cosmetica_cats != "": producto.guia_cosmetica_cats = guia_cosmetica_cats try: producto.save() except ValueError: return Response({"success": False, "msg": "Value Error"}) except ObjectDoesNotExist: return Response({"success": False, "msg": "Product does not exist"})
Kommentarer
- Denna fråga är ofullständig. För att hjälpa granskare att ge dig bättre svar, lägg till tillräckligt med sammanhang i din fråga. Ju mer du berättar om vad din kod gör och vad syftet med det är, desto lättare blir det för granskare att hjälpa dig. Frågorna ska innehålla en beskrivning av vad koden gör
Svar
Du kan använda .update()
förfrågningsmetod och packa upp request.data
ordbok:
product.objects.filter(pk=product_id).update(**request.data)
Eller så kan du gå över request.data
artiklar och använd setattr()
inbyggd funktion :
producto = Product.objects.get(pk=product_id) for attr, value in request.data.items(): if value: # check if value is "truthy" - you may not need this check, please test setattr(producto, attr, value) producto.save()
Observera att du kan och borde hantera ”slugify” -delen på olika sätt – till exempel åsidosätta modellen ”s save()
-metoden och skapa en slug om en titulo
ges – exempel här .
Kommentarer
- Wow, att ' är fantastiskt! Det ' är säkert mot SQL-injektion, eller hur?
- @MarcosAguayo absolut, det går genom Django ORM och jag s injektionsskyddad. Tack.
Lämna ett svar