How can I improve this? It's large and ugly. Any advice?
I'm receiving a POST from a API, and I want to update only the fields that are not 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"})