I have a form where I already insert some data through the following code in views.py
def crear(request):
form = RecForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
At the same time that the form data is registered, I also want to update other fields that are not available to the user but that must be updated within the application.
Could you give me an idea of where I should place the code to insert at the same time as the registration of the form?
Thanks.
def crear(request):
form = RecForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
# additional logic goes here
In the form.save method:
class RecForm(forms.ModelForm):
# your fields go here
def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
# additional logic goes here
return instance
Or create a function to handle all this, and call it on your view
def sign_up(name, age, other_arguments_you_need):
# Logic goes here
hello,
Perfect, now I would ask where it is usually recommended to add the code.
Besides, let’s imagine that I want to add it to the view part, I’m not very sure how to collect the id value (from the record that the form saves) so that the fields that I modify internally are those of the same record that the form sends me.
Thanks.
I would add that the best answer for this is “it depends” - usually upon the size and complexity of the code needing to do the work, and how this logic integrates with the rest of your system.
Placing it in the view is fine, if it’s just a few lines of code. Beyond that, I’d be looking to put it in an external function.
I would consider adding it to the form’s save method if it’s logic that would always be used with that form. (We tend to create generic forms that can handle multiple situations, so something like that would not work for us.)
In my case, this form is used to enter some records, to those records values must always be inserted in some fields that the application must do internally, that for each record.
I have used objects such as object.get, object.All and they have worked for me but I am failing in something so that I cannot get the value I want or I don’t know where to put it.
If in a new view I add the code
def NonConformity(request):
claims = rec.objects.all()
for Rec in claims:
print(f"Claim: {Rec.id_reclamation}")
return render(request,'claims/Nonconformity.html')
It shows me the different records but if I want to take out only the record that is being inserted right in the form, it doesn’t work out.
Do you know where the error could be?
Thank you.
Have you worked your way through the official Django tutorial? It teaches you how you query for individual rows in your models and how to change and update them.
Yes, but I don’t quite understand how to collect the id of the record without me passing it to them manually.
The query has worked for me, passing the value manually, but if I have to pick it up from a record recently added by form, I can’t.
That is, just after inserting the record in the database, I add objects to collect records, and it effectively shows me all the records, including the one that has been recorded at that moment.
form = RecForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
claims = rec.objects.all()
for Rec in claims:
print(f"Claim: {Rec.id_reclamation}")
return redirect('Claims')
I only want the last one, because this last one is where I am going to modify some field.
I’ve been looking since yesterday and it doesn’t come out, I’m not understanding something…
def create(request):
form = RecForm(request.POST or None, request.FILES or None)
if form.is_valid():
id = form.instance.pk # Your id of the record you inserted.
a = rec.objects.get(pk=id)
b5.status = '11111'
form.save()
I get the id of the record but I am not able to insert in the “status” field what I want.
Let’s see if you can give me a hand.
So the code to add that I want would have to go after form.save()?
2- Pay no attention because it is from a test that I was doing and that obviously has not worked for me.
3- I don’t know exactly what you mean by that, something that does come up is knowing the id of the record that has been added to the database.
I understand that save is an insert
I can do the following to save form data even though I don’t quite understand the instance key:
f = RecForm(request.POST, instance=id)
f.save()
The id of the record that you entered in the database will also be collected:
id = form.instance.pk # Your id of the record you inserted.
But there I stay, I don’t know how to continue, I don’t see or I don’t know how to see an example when I want to update a field of that record. I also do not understand very well if I have to add the field that I want to update in the form class or not
class RecForm(forms.ModelForm):
class Goal:
model=rec
fields = ['complaint_date', 'complaint_num', 'customer_num']