insert mysql database record

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.

There are plenty ways of doing it:

In the view itself:

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.

The form.save method returns the instance that was saved.

There are many opinions on where you should put it. Django recommends to put on the view.

[opinion]
I’m actually following this style guide.
[/opinion]

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.)

Sorry for asking but I’m a little lost, would it be something like that?

def create(request):
      form = RecForm(request.POST or None, request.FILES or None)
      if form.is_valid():
          form.save()
          rec(status='12345678')

this does nothing for me…
rec is the table and status is the field I want to modify.

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.

Review the docs at

Again, it’s a decision for you to make within the context of your system.

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.

See the docs for the save method.

The .save method on a ModelForm returns the instance of the model that was just saved.

Hello,

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.

Thanks.

There are a number of different things wrong here.

1 - you haven’t saved the form yet, this only works if you’re updating existing data.

2 - Where does this “b5” object come from? I don’t see where it’s defined or created.

3 - As mentioned before, the .save method on a ModelForm returns the instance of the model that was just saved.

  1. 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.

Have you read the docs (and examples!) at the save method?

If you have, and still aren’t understanding something, please identify what part of the doc is not clear to you.

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']

The second example block of the referenced docs show exactly what you’re trying to do here.

Hello,
Now if it has worked, I was missing the issue of commit=False that I didn’t quite understand.
Many thanks for everything!