[Answer]-Django exclude fields in dictionary that are not model attributes

1👍

Create a ModelForm for your model, and let it handle that logic.

This is what ModelForm‘s are for, and this is how you’re supposed to be solving that problem:

# forms.py
from django.forms import ModelForm
from myapp.models import Article


class ApplicationForm(ModelForm):
    class Meta:
        model = Application


# views.py

def my_view(request):
    if request.method == "POST":
        form = ApplicationForm(request.POSt)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/thanks/")
    form = ApplicationForm()
    return render(request, 'template.html', {'form': form})

Leave a comment