Form fields are empty when created using instance = instance

Viewed 100

I have two models prodcut_prices and WrongPrice.

In WrongPrice the user can correct report wrong prices - when the price is reported it should also be updated in product_price.

My problem is, even though I instantiate product_price at the very beginning as instance_productprice, all of its required fields returns the "this field has to be filled out" error. How come those field are not set when im using the instance instance_productprice = product_prices.objects.filter(id=pk)[0] ? Note, that all fields in product_prices are always non-empty since they are being pulled from the product_price model, which is handled in another view, thus that is not the issue.


def wrong_price(request,pk):
     
    #Get the current price object
    instance_productprice = product_prices.objects.filter(id=pk)[0]
     
    #Get different values
    wrong_link = instance_productprice.link 
    img_url = instance_productprice.image_url 
    wrong_price = instance_productprice.last_price
    domain = instance_productprice.domain
        
    # Create instances
    instance_wrongprice = WrongPrice(
    link=wrong_link,
    correct_price=wrong_price,
    domain = domain)


    
    if request.method == "POST":
        form_wrong_price = wrong_price_form(request.POST,instance=instance_wrongprice)
        
        # Update values in product_prices
        form_product_price = product_prices_form(request.POST,instance=instance_productprice)
        form_product_price.instance.start_price = form_wrong_price.instance.correct_price
        form_product_price.instance.last_price = form_wrong_price.instance.correct_price

        
        if form_wrong_price.is_valid() & form_product_price.is_valid():
            form_wrong_price.save()
            form_product_price.save()
            
            messages.success(request, "Thanks")
            return redirect("my_page")
        else:
            messages.error(request, form_product_price.errors) # Throws empty-field errors, 
            messages.error(request, form_wrong_price.errors)
            return redirect("wrong-price", pk=pk)
    else:
        form_wrong_price = wrong_price_form(instance=instance_wrongprice)
        return render(request, "my_app/wrong_price.html",context={"form":form_wrong_price,"info":{"image_url":img_url}})


1 Answers

I am bit confused about how you implemented it. You have passed a instance of WrongPrice price through the form, which is unnecessary, you could have used initial:

wrong_values = dict(
    link=wrong_link,
    correct_price=wrong_price,
    domain = domain
)

form_wrong_price = wrong_price_form(initial= wrong_values)

Then you are adding values to product_prices_form from instance of form_wrong_price. I don't see why you need a form again here. You can simple use:

form_wrong_price = wrong_price_form(request.POST, initial= wrong_values)
if form_wrong_price.is_valid():
    instance = form_wrong_price.save()
    instance_productprice.start_price = instance.correct_price
    instance_productprice.last_price = instance.correct_price
    instance_productprice.save()

Finally, please use PascalCase when defining class names. And you can get the product prices by product_prices.objects.get(id=pk)(instead of filter()[0]).

Related