How can I allow user to submit a form

Question:

When I try to submit Table model to the database using create_table view, it throws me an error: NOT NULL constraint failed: audioApp_table.user_id after I do my own research I found out it was because I didn’t add user to the form, so I try to add it by adding: table = Table(user=request.user), but it is not working, how can we allow user to submit this form, I knew how to do that in class base view using instance.user = self.request.user, but for function base view I’m failed.

def create_table(request):
    columns = Column.objects.filter(user=request.user)

    fields = {}
    for column in columns:
        if column.field_type.data_type == 'number':
            fields[column.name] = forms.IntegerField()

        elif column.field_type.data_type == 'character':
            fields[column.name] = forms.CharField(max_length=20)

        elif column.field_type.data_type == 'decimal':
            fields[column.name] = forms.DecimalField(max_digits=20, decimal_places=10)

        elif column.field_type.data_type == 'image':
            fields[column.name] = forms.ImageField()

        elif column.field_type.data_type == 'boolean':
            fields[column.name] = forms.BooleanField()

        elif column.field_type.data_type == 'date':
            fields[column.name] = forms.DateField()
    TableForm = type('TableForm', (forms.Form,), fields)

    if request.method == 'POST':
        form = TableForm(request.POST, request.FILES)

        if form.is_valid():

            table = Table()

            for column in columns:
                setattr(table, column.name, form.cleaned_data[column.name])
                table.save()
                return redirect('Table')
    else:
        form = TableForm()
    return render (request, 'create_table.html', {'form':form})
class Table(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    number = models.IntegerField(blank=True, null=True)
    decimal = models.DecimalField(max_digits=20, decimal_places=10, blank=True, null=True)
    image =  models.ImageField(upload_to='table-image', blank=True, null=True)
    character = models.CharField(max_length=500, blank=True, null=True)
    check_box = models.BooleanField(blank=True, null=True)
    date = models.DateField(blank=True, null=True)

Answers:

You need to set the user for the Table instance before saving it to the database. You can do this by setting table.user to request.user after creating the table instance in your create_table view function

 if request.method == 'POST':
        form = TableForm(request.POST, request.FILES)

        if form.is_valid():
            table = Table()
            table.user = request.user 

            for column in columns:
                setattr(table, column.name, form.cleaned_data[column.name])

            table.save()
            return redirect('Table')
    else:
        form = TableForm()

    return render(request, 'create_table.html', {'form': form})
Answered By: Abdulmajeed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.