1
\$\begingroup\$

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"})
\$\endgroup\$
  • 1
    \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ – 409_Conflict Mar 16 '17 at 9:42
4
\$\begingroup\$

You can use .update() queryset method and unpack the request.data dictionary:

product.objects.filter(pk=product_id).update(**request.data)

Or, you may loop over request.data items and use the setattr() built-in function:

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()

Note that you can and should handle the "slugify" part differently - for example, overriding the model's save() method and creating a slug if a titulo is given - example here.

\$\endgroup\$
  • \$\begingroup\$ Wow, that's awesome! It's secure against SQL Injection right? \$\endgroup\$ – Marcos Aguayo Mar 16 '17 at 18:07
  • 1
    \$\begingroup\$ @MarcosAguayo absolutely, it goes through the Django ORM and is injection-safe. Thanks. \$\endgroup\$ – alecxe Mar 16 '17 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.