0

This is my view:

@login_required
def createPackage(request):
    if request.method == "POST":
        print(request.POST)
        p_form = packageCreateForm(request.POST, instance=request.user)
        if p_form.is_valid():
            p_form.save()
            x = p_form.cleaned_data.get('pickup_phoneNumber')
            y = p_form.cleaned_data.get('item')
            print(x,y)
            messages.success(request, f'Hi {request.user.username} you have successfully created an order with POST BOX oure customer care reperesntative will reach out to you soon ')
            return redirect('post-box-home')
        else:
            print("-----Form is not valid-----")
            
    else:
        p_form = packageCreateForm()
    return render(request, 'packages/createPackage.html', {'p_form': p_form})

This is the form:

from django import forms
from .models import Package



class packageCreateForm(forms.ModelForm):
    class Meta:
        model = Package
        fields = ['item', 'description', 'weight', 'quantity', 'Contained_In', 'pickup_phoneNumber', 'delivery_phoneNumber',  'pickup_address','delivery_address']

This is the model:

class Package(models.Model):
    customer=models.ForeignKey(User, on_delete=models.CASCADE )
    item = models.CharField(max_length=250)
    description= models.TextField()
    package_no =models.IntegerField(default=0)
    weight = models.FloatField()
    quantity= models.IntegerField(default=1)
    drop_off = models.BooleanField(default=False)
    pickup_address= models.TextField()
    Contained_In = models.CharField(choices=CONTAINED_LIST, max_length=3, help_text='Select the package you item can be contained in', default= "")
    delivery_address= models.TextField()
    pickup_phoneNumber= PhoneNumberField(default = '')
    delivery_phoneNumber= PhoneNumberField(default = '')
    pickup_price=models.FloatField(default=0.00)
    delivery_price=models.FloatField(default=0.00)
    package_status=models.CharField(choices=PACKAGE_STATUS_LIST, max_length=3,help_text='Select a status for this pacakage', default= "")
    request_date = models.DateTimeField(auto_now_add=True)
    updated_date = models.DateTimeField(auto_now=True)
    # note = models.ManyToManyField(User, on_delete=models.CASCADE )

    def __str__(self):
        return self.item

I really don't know what I am doing wrong the form pass the valid test but nothing is saving on the database.

Please I have also share the model, I don't know if it could be from there.

3
  • Also share Package model. Why you are passing instance=request.user? Commented Aug 23, 2022 at 5:19
  • thats because the model is having an owner field thats link to user Commented Aug 23, 2022 at 5:24
  • I only see customer field not owner there in the model. Commented Aug 23, 2022 at 5:34

1 Answer 1

2

Package model has customer a ForeignKey field, you are passing instance=request.user in the packageCreateForm and not handing it in the constructor, i.e. in the __init__() of the form, so you first do commit=False then save it's customer field, so:

@login_required
def createPackage(request):
    if request.method == "POST":
        print(request.POST)
        p_form = packageCreateForm(request.POST)
        if p_form.is_valid():
            package=p_form.save(commit=False)
            package.customer=request.user
            package.save()
            x = p_form.cleaned_data.get('pickup_phoneNumber')
            y = p_form.cleaned_data.get('item')
            print(x,y)
            messages.success(request, f'Hi {request.user.username} you have successfully created an order with POST BOX oure customer care reperesntative will reach out to you soon ')
            return redirect('post-box-home')
        else:
            print("-----Form is not valid-----")
            
    else:
        p_form = packageCreateForm()
    return render(request, 'packages/createPackage.html', {'p_form': p_form})

Note: classes are written in PascalCase so you may write it as PackageCreateForm rather than packageCreateForm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.