Reputation: 155
I am new in Django forms and I need to insert/update some data into my database.
I have some model and in the django admin panel I introduce manually the user's phone and the IMEI number. After that I create a form, a template.html and a view.
The form is as follows:
from django import forms
class Send2phone(forms.Form):
NumberOfCalls = forms.CharField(
min_length=1,
widget=forms.TextInput({'class': 'form-control'})
)
TimeBetweenCalls = forms.CharField(
widget=forms.TextInput({'class': 'form-control'})
)
PSAP = forms.CharField(
min_length=1,
widget=forms.TextInput({'class': 'form-control'})
)
And my view is:
def phone_config(request):
if request.method == 'POST':
form = Send2phone(request.POST)
if form.is_valid():
cleaned_data = form.cleaned_data
NumberOfCalls = cleaned_data.get('NumberOfCalls')
TimeBetweenCalls = cleaned_data.get('TimeBetweenCalls')
PSAP = cleaned_data.get('PSAP')
phone_model = Phone()
phone_model.id = 1
phone_model.user = donothing
phone_model.imei = donothing
phone_model.num_calls = NumberOfCalls
phone_model.time_btwn_calls = TimeBetweenCalls
phone_model.psap = PSAP
phone_model.save()
return redirect(reverse('gracias'))
else:
form = Send2phone()
return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})
def gracias_view(request):
return render(request, 'heroconfigurer/gracias.html')
My problem comes now when I create the view. First of all, I check if the method is post and I get the data from the form. Then I check if the form is valid and I create the object Phone. After that assign the different parameters and save them. Inserting the data from the form is working good but imei and user ara being deleted if I don't specify them.
How can I insert data in the database models where exist some users and imeis? For example in id=1 I already have a user and an imei and I want to keep them
Upvotes: 1
Views: 2893
Reputation: 20539
Existing fields are erased because you're creating new Phone
object with existing id. You should instead retrieve existing Phone
model and update it:
phone_model = Phone.objects.get(id=1)
phone_model.num_calls = NumberOfCalls
phone_model.time_btwn_calls = TimeBetweenCalls
phone_model.psap = PSAP
phone_model.save()
Or update it using queryset update method:
Phone.objects.filter(id=1).update(
num_calls=NumberOfCalls,
time_btwn_calls=TimeBetweenCalls,
psap=PSAP,
)
First one will touch database twice. Once for loading existing phone, and then for saving new values. Second will touch database only once, updating fields without touching rest of them.
Upvotes: 1
Reputation: 599450
You should be retrieving the existing Phone object and updating it where necessary.
if form.is_valid():
number_of_calls = form.cleaned_data.get('NumberOfCalls')
time_between_calls = form.cleaned_data.get('TimeBetweenCalls')
psap = form.cleaned_data.get('PSAP')
phone = Phone.objects.get(pk=1)
phone.num_calls = number_of_calls
phone.time_btwn_calls = time_between_calls
phone.psap = psap
phone.save()
Even better, make your form a ModelForm, and include only the fields you want to update:
class Send2phone(forms.ModelForm):
class Meta:
model = Phone
fields = ['num_calls', 'time_btwn_calls', 'psap']
now your view is just:
phone = Phone.objects.get(pk=1)
if request.method == 'POST':
form = Send2tcu(request.POST, instance=phone)
if form.is_valid():
form.save()
return redirect(reverse('gracias'))
else:
form = Send2tcu(instance=phone)
return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})
Upvotes: 1