Mar
Mar

Reputation: 701

django invalid form in user field

views.py

def view_form(request, template_name='form.html'):
    user = request.user
    if request.method == "POST":
        form = my_Form(request.POST or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user_mytable = request.user
            instance.save()
        else:
            print('invalid')
    else:
        form = my_Form()
    return render(request, template_name,{'form':form})

forms.py

class my_Form(forms.ModelForm):
    class Meta:
        model = my_table
        fields = ('__all__')

models.py

class my_table(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    user_mytable = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    field1 = models.FloatField(blank=True, null=True)
    field2 = models.FloatField(blank=True, null=True)
    field3 = models.TextField(max_length=150,blank=True, null=True)
    time = models.TextField(blank=True, null=True)

i get invalid form in user_mytable This field is required.

any idea how to fix it ?because i use request.user

Upvotes: 2

Views: 372

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477523

You did not exclude the field from the form, so that means it is included in the form, even if you do not render it. By default all fields are required. You can set the field to non-editable by setting editable=False:

class my_table(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    user_mytable = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        editable=False
    )
    field1 = models.FloatField(blank=True, null=True)
    field2 = models.FloatField(blank=True, null=True)
    field3 = models.TextField(max_length=150,blank=True, null=True)
    time = models.TextField(blank=True, null=True)

an alternative is to exclude it from the fields:

class my_Form(forms.ModelForm):
    class Meta:
        model = my_table
        exclude = ('user_mytable',)

In the view, you can then set the user_mytable field of the object:

from django.contrib.auth.decorators import login_required

@login_required
def view_form(request, template_name='form.html'):
    user = request.user
    if request.method == 'POST':
        form = my_Form(request.POST, request.FILES)
        if form.is_valid():
            form.instance.user_mytable = request.user
            form.save()
            return redirect('name-of-some-view')
        else:
            print('invalid')
    else:
        form = my_Form()
    return render(request, template_name,{'form':form})

Note: You can limit views to a view to authenticated users with the @login_required decorator [Django-doc].


Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.

Upvotes: 1

Related Questions