form.instance.add or set django code example
Example 1: import forms
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
Example 2: passing user instance django form submission
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
Example 3: django model form
class YourForm(ModelForm):
class Meta:
model = YourModel
fields = ['pub_date', 'headline', 'content', 'reporter']