I'm attempting to implement a multi-step form on Django and am relatively new to the language but am having difficulty when it comes to saving to the data to the backend. Any help would be much appreciated. Spend around 8 hours trying to figure this out myself.
I need to pass the code and name from model Books to People and then insert the other required information on step 2 as this is a separate table model.
There is a foreign key connecting People to Books via the id.
Views.py
#Step1
def step1(request):
initial={'code': request.session.get('code', None),
}
form = PrimaryForm(request.POST or None, initial=initial)
if request.method == 'POST':
if form.is_valid():
return HttpResponseRedirect(reverse('main_app:step2'))
return render(request, 'step1.html', {'form': form})
#Step2
def step2(request):
form = SecondaryForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form1 = form.save(commit=False)
form2 = People.objects.create(code=request.session['code'])
form1.save()
return HttpResponseRedirect(reverse('main_app:finished'))
return render(request, 'step2.html', {'form': form})
Forms.py
#models/forms
class PrimaryForm(forms.ModelForm):
class Meta:
model = People
fields = ('id','code','name',)
class SecondaryForm(forms.ModelForm):
class Meta:
model = Books
fields = ('type','title',)
exclude = ('book_id',)
Error message
insert or update on table "PEOPLE" violates foreign key constraint
DETAIL: Key (id_id_id)=(0) is not present in table "PEOPLE".
The code, name should pass through the forms and at step 2 save the information from both forms.
Related
Let's say I submit a form to the back-end and I save a record of the model in the following way:
views.py:
def viewName(request):
if request.method == 'POST':
form = ProjectForm(request.POST)
if form.is_valid():
form.save() #I want to get the id of this after it is saved
else:
print (form.errors)
form = ProjectForm()
return render(request, 'index.html', context)
forms.py:
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = '__all__'
Right after saving the form, I would like to get the id of the record for the model.
I tried with form.id and form.pk as I saw in other similar questions without success.
How can I get the id or the pk of the new entry added to the Project model?
form.save() returns the object, so:
obj = form.save()
print(obj.pk)
I'm trying to allow users to upload an image. When users are first created, they are given a unique ID / primary key. When users upload an image, I want to save that image in a folder depending on what the users unique ID is. For example, if the users unique ID is 1, I want to save it in
1/uploadedPhotos/imageName
This is my model:
def get_file_path(instance, filename):
return os.path.join('%s/uploadedPhotos' % instance.user_id, filename)
class UserImages(models.Model):
user = models.ForeignKey(User)
photo = models.ImageField(upload_to=get_file_path)
and this is my form:
class UploadImageForm(forms.ModelForm):
class Meta:
model = UserImages
fields = ['photo']
and this is my view:
def uploadImageView(request):
if request.method == 'POST':
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
# file is saved
form.save()
return redirect('/')
else:
form = UploadImageForm()
return render(request, 'uploadImagePage.html', {'uploadImageForm': form})
The URL which calls the uploadImageView view is /uploadImage/. when I go to that URL and upload an image using the uploadImageForm, it gives an error saying:
IntegrityError at /uploadImage/
null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (1, null, None/uploadedPhotos/imageName.png).
and the traceback leads back to the
form.save()
line in my uploadImageView. What am I doing wrong to cause this error?
Your UserImages model requires user but your form UploadImageForm is asking only asking for photo. You need to set user, try something like this:
def uploadImageView(request):
if request.method == 'POST':
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
# file is saved
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('/')
else:
form = UploadImageForm()
return render(request, 'uploadImagePage.html', {'uploadImageForm': form})
obj = form.save(commit=False)
obj.user = request.user
obj.save()
You must extract user from request.user and add it to form data.
I'm a Python/Django newbie and have been facing the following problem for several days now. I've got 2 ModelForms for a Model and the idea is that the data for one will help populate the multiplechoicefield of the second. The multiplechoice field for the second form populates ok but the problem is that the user's choices don't get saved. I've tried the save(commit=false) and save_m2m() but it hasn't fixed the issue.
models.py:
class Question(models.Model):
asker = models.ForeignKey(Student, related_name='asker+')
suggestedHelpers = models.ManyToManyField(Student, related_name='suggested')
chosenHelpers = models.ManyToManyField(Student, related_name='chosen')
class QuestionForm(ModelForm):
class Meta:
model = Question
exclude = ('chosenHelpers','suggestedHelpers', 'asker',)
class HelpersForm(ModelForm):
def suggesthelpers(self, question):
# populate chosenHelpers field according to data from QuestionForm
class Meta:
model = Question
exclude = ('suggestedHelpers', 'asker', 'questionSubj', 'Qtext',)
views.py
def askq_page(request):
question = Question(asker=Student.objects.get(user=request.user))
if request.method == 'POST':
form = QuestionForm(request.POST, instance=question)
if form.is_valid():
question = form.save()
# process data
question.save()
form2 = HelpersForm(instance=question)
form2.suggesthelpers(question)
variables = {'form':form2, 'qid':question.id}
return render_to_response('choosehelper.html', variables,context_instance=RequestContext(request))
else:
variables = RequestContext(request, {'form': QuestionForm()})
return render_to_response('askq.html', RequestContext(request, {'form': QuestionForm(instance=question)}))
def choosehelper_page(request, form='', qid=''):
question = Question.objects.get(id=qid)
if request.method == 'POST':
form2 = HelpersForm(request, instance=question)
# here lies the problem- the data gathered from form2 is not saved i.e. the multiple choices chosen by user are not returned
if form2.is_valid():
question = form2.save()
form2.save_m2m()
helpers = question.chosenHelpers.all()
for helper in helpers:
sentQ = ReceivedQ(question=question)
sentQ.student = helper
sentQ.save()
return render_to_response('questionsent.html', {'helpers': helpers, 'qid': question.id, 'qtext': q}, context_instance=RequestContext(request))
else:
form2 = HelpersForm(instance=question)
form2.suggesthelpers(question)
return render_to_response('choosehelper.html', RequestContext(request, {'form': form2, 'qid': qid}))
I'm doing something wrong here, but I can't find it.
I'm using a model form:
class ArtistInfo(ModelForm):
class Meta:
model = Onesheet
fields = (
'name',
'genre',
'location',
'biography',
)
And trying to save the data entered for an existing record.
def edit_artist_info(request, onesheet):
onesheet = Onesheet.objects.get(id = onesheet)
if request.method == 'POST':
form = ArtistInfo(request.POST, instance=onesheet)
if form.is_valid():
test = form.save(commit=False)
test.save()
HttpResponseRedirect('/')
form = ArtistInfo(instance=onesheet, label_suffix='')
variables = RequestContext(request, {
'onesheet':onesheet,
'form': form,
})
return render_to_response('edit_artist_info.html', variables)
But it's not saving. It just reloads the page with whatever the user changed, but if you actually refresh the page (grabbing the value from the DB), it's the old value.
Any ideas? If it's because the form isn't actually validating, I dont know why it's not validating.
try just
if request.method == 'POST':
form = ArtistInfo(request.POST, instance=onesheet)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
You were missing the return statement in your code, and the extra save() was unnecessary
Possibly a newbie question, so please bear with me.
I have a Django form that edits a certain instance of a Model. In order to know which object is being edited, I have a hidden field containing the id of the object, along with the URL containing the id.
First question: Is having the id of the object in a hidden field the right way of doing it?
My (possibly unfounded) concern with having it only as part of the url is that someone could then open the page of one object id, submit the form to another, and that object will then be overwritten. That's why I'm trying to use a hidden field.
The problem with storing the id in a hidden field is that, on validation of the form, Django complains that the object does not have an unique id (obviously).
Second question: If a unique field is part of a form, how does one tell Django to ignore the fact that that key already exists, in order to update the object?
Why don't you just use ModelForm?
# forms.py
# ...
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
# ...
def my_view(request, id):
instance = get_object_or_404(MyModel, id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return render(request, 'my_template.html', {'form': form})
See https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/#the-save-method for more details.
Update for Django 1.6 and further version
# forms.py
# ...
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
def my_view(request, id):
instance = MyModel.objects.get(id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return direct_to_template(request, 'my_template.html', {'form': form})