Answer the question
In order to leave comments, you need to log in
How to assign the current user in the user field?
In my project I have a form with access to filling only for authorized users.
Here are the approximate fields of the model on which the form is built:
number=models.PositiveSmallIntegerField()
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
gogs=models.ForeignKey('dog', on_delete=models.CASCADE, blank=True)
zones=models.ManyToManyField('zoness')
мой views.py:
from .forms import DogFormSet
def dog(request):
formset = DogFormSet (queryset=dog.objects.none())
if request.method == 'POST' and 'speed' in request.POST:
form2 = SpeedsForms(request.POST)
if form2.is_valid():
form2.save()
if request.method == 'POST' and 'dogs' in request.POST:
formset = DogFormSet(request.POST)
if formset.is_valid():
formset.save()
if request.method == 'POST' and 'color' in request.POST:
form4 = ColorForms(request.POST)
if form4.is_valid():
form4.save()
if request.method == 'POST' and 'Req' in request.POST:
form5 = DogRequestsForm(request.POST)
if form5.is_valid():
form5.save()
form2 = SpeedsForms()
form4 = ColorForms()
form5 = DogRequestsForm()
data = {
'form2': form2,
'formset': formset,
'form4': form4,
'form5': form5,
}
return render(request, 'main/dogsform.html', data)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question