I create several forms to edit some records. When I save the form, I can see in the database that the application update the record and insert one or two record more.
All the forms that I use to update record have this weird behavior. I don't know if the error is in the form definition or in the view definition.
Model
class DetalleRecepcion(models.Model):
id_proveedor = models.ForeignKey(Proveedor,db_column='id_proveedor',primary_key=True, verbose_name='Proveedor')
anio = models.IntegerField( null=False)
mes = models.IntegerField(verbose_name='Mes')
fecha_recepcion = models.DateField(verbose_name='Fecha Recepcion')
usuario = models.CharField(max_length=15, blank=True)
num_archivos = models.IntegerField(primary_key=True, verbose_name='No de archivos')
class Meta:
managed = False
db_table = 'mpc_detalle_recepcion'
View
#login_required(login_url='/login/')
def DetRecView(request):
idp = request.GET.get('i')
anio = request.GET.get('a')
mes = request.GET.get('m')
if request.method == 'POST':
r = DetalleRecepcion.objects.get(id_proveedor=idp,anio=anio,mes=mes)
form = DetRecForm(request.POST or None, instance =r)
if form.is_valid():
form.save()
return HttpResponse('<script type="text/javascript">window.close()</script>')
else:
r = DetalleRecepcion.objects.get(id_proveedor=idp,anio=anio,mes=mes)
r.usuario = request.user
form = DetRecForm(instance=r)
return render_to_response('detrec.html',
{'form':form},
context_instance=RequestContext(request))
Form
class DetRecForm(forms.ModelForm):
fecha_recepcion = forms.DateField(widget=DateInput(),)
def __init__(self,*args,**kwargs):
super(DetRecForm,self).__init__(*args,**kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Field('id_proveedor',
'anio',
'mes',
'usuario',
readonly = True
),
Fieldset('',
'fecha_recepcion',
'num_archivos',
Submit('save','Grabar'),
HTML('<a class="btn btn-danger" id="cerrar">Cancelar</a>')
)
)
class Meta:
model = DetalleRecepcion
I am using a legacy database, I check the constrains, procedure, triggers of the database and everything look fine.
Beside the table don't use any procedure function etc.
And only when I update or insert from the application I can see this behavior.
UPDATE
urls.py
urlpatterns = patterns('',
url(r'^recepcion/$','prov.views.DetRecView',name='recepcion'),
url(r'^newdetrec/$','prov.views.NewDetRecView',name='newdetrec'),
url(r'^master/$','prov.views.NewMasterView',name='master'),
url(r'^conci/$', 'prov.views.ConciView',name='conci'),
url(r'^carga/$', 'prov.views.CargaView',name='carga'),
url(r'^gencert/$', 'prov.views.GenCertView',name='gencert'),
url(r'^entcert/$', 'prov.views.EntCertView',name='entcert'),
url(r'^aceptacert/$', 'prov.views.AceptaCertView',name='aceptacert'),
url(r'^envconci/$', 'prov.views.EnvConciView',name='envconci'),
)
My create view (for the same model)
#login_required(login_url='/login/')
#permission_required('prov.views.configView',login_url='/login/')
def NewDetRecView(request):
form = NewDetRecForm(request.POST or None)
if request.method == 'POST':
idp = request.POST['id_proveedor']
a = request.POST['anio']
m = request.POST['mes']
id = Proveedor.objects.get(id_proveedor=idp)
obj,created = DetalleRecepcion.objects.get_or_create(id_proveedor=id,anio=a,mes=m)
obj.save()
return HttpResponseRedirect('/monitor/')
if not created:
obj.id_proveedor = id
obj.anio = a
obj.mes = m
obj.save()
return HttpResponseRedirect('/monitor/')
return render_to_response('newdetrec.html',
{'form':form})
My form class to create new records:
class NewDetRecForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(NewDetRecForm,self).__init__(*args,**kwargs)
self.helper = FormHelper(self)
self.helper.layout.append(Submit('save','Grabar'))
self.helper.layout = Layout(
Fieldset('',
'id_proveedor',
'anio',
'mes',
Submit('save','Grabar'),
)
)
def clean(self):
cleaned_data = super(NewDetRecForm, self).clean()
id_proveedor = self.cleaned_data['id_proveedor']
#num_archivos = self.cleaned_data['num_archivos']
anio = self.cleaned_data['anio']
mes = self.cleaned_data['mes']
qs = self.Meta.model.objects.filter(id_proveedor=id_proveedor, anio=anio, mes=mes)
if self.instance:
qs = qs.exclude(pk = self.instance.pk)
if qs.count() > 0:
raise forms.ValidationError(u'Registro ya existente')
return cleaned_data
class Meta:
model = DetalleRecepcion
DetRecForm is for update
DetRecView is for update
NewDetRecForm is for create
NewDetRecView is for create
UPDATE 2
javascript function to pass parameters
<script type="text/javascript">
$(document).ready ( function () {
$(document).on ("click", "#recepcion", function (event) {
event.preventDefault();
var tbl = document.getElementById("myTable");
var idpro = $(this).parents('tr:first').find('td:first').text();
var anio = $(this).closest('tr').children(":eq(1)").text();
var mes = $(this).closest('tr').children(":eq(2)").text();
var argu = "?i="+idpro+"&a="+anio+"&m="+mes;
//window.location = "/recepcion/"+argu;
var url = "/recepcion/"+argu;
window.open(url,'_blank')
});
});
</script>
I know this isn't the right way to pass parameters to the templates.
I am start learning AJAX to pass the data, but meanwhile I use this horrible function
url(r'^recepcion/add/$','prov.views.DetRecView',name='recepcion_add'), # create object
url(r'^recepcion/edit/(?P<pk>\d+)/$','prov.views.DetRecView',name='recepcion_edit'), # update object
Lets look at your condition in the view function: if request.method=='POST' better use if request.POST,
So when you have else - it means request.GET you get an instance of DetalleRecepcion (read instance as object of some class) and pass it to your ModelForm. It is update object approach (but you use this to create object), and provided query must give one object (unique).
If you want use this view to create and update you have to change your urls (smth like I did above) and you have to define condition: if request.kwargs.get('pk') you're going to update object else you're going to create object.
Main difference that if it is update case you have to provide instance to your form r = DetalleRecepcion.objects.get(pk=request.kwargs['pk']), so DetRecForm(instance=r) (whenrequest.GET) and form = DetRecForm(request.POST, instance =r) (whenrequest.POST).
If you want provide some initial data to your form (when request.GET) use DetRecForm(initial = {'id_proveedor': idp, 'anio': anio, 'mes': mes, 'usuario': request.user})
Related
I have this view that creates a form with groups and exercises.
How can I do to be able to create more groups and exercises in the template?
views.py
#login_required
def creaScheda(request):
if request.method == "POST":
form = CreaSchedaForm(request.POST)
if form.is_valid():
schedaName = form.cleaned_data['nome_scheda']
scheda = form.save(commit = False)
scheda.utente = request.user
scheda.save()
gruppi = DatiGruppi(
giorni_settimana = form.cleaned_data['giorni_settimana'],
dati_gruppo = form.cleaned_data['dati_gruppo'],
gruppi_scheda = Schede.objects.get(nome_scheda = schedaName)
)
gruppi.save()
esercizi = DatiEsercizi(
serie = form.cleaned_data['serie'],
ripetizione = form.cleaned_data['ripetizione'],
peso = form.cleaned_data['peso'],
gruppo_single = DatiGruppi.objects.get(gruppi_scheda = scheda.id),
dati_esercizio = form.cleaned_data['dati_esercizio']
)
esercizi.save()
return redirect('/backoffice')
else:
form = CreaSchedaForm()
context = {"form": form}
return render(request, "crea_scheda.html", context)
A solution to this would be using the bulk_create method on the object manager with an array/list of the objects to be created
example
ModelName.objects.bulk_create([
ModelName(title="Model1"),
ModelName(title="Model2"),
ModelName(title="Model3"),
])
where ModelName refers to the Name of Your Model, and ModelName within the list refers to the different instances/records of the ModelName class/database to be create in a bulk
I have one model Measurement, two forms MeassurementSystolicPressureForm and MeassurementDiastolicPressureForm. I want to make a view that allows user to add both of them to the database. Each has fields: username, measurement_date, value, measurement_type. When I fill forms on my webpage two records are added to the db, each has a good username and measurement_type, but measurement_date and value are the same for both records. Can you help me spotting what I'm doing wrong?
Here is my code:
models.py
class Measurement(models.Model):
value = models.IntegerField()
measurement_type = models.CharField(max_length=6, default='measurement', blank=True)
username = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
measurement_date = models.DateTimeField(default=datetime.now, editable=True)
forms.py
class MeassurementSystolicPressureForm(ModelForm):
class Meta:
model = Measurement
fields = ['value', 'measurement_date']
class MeassurementDiastolicPressureForm(ModelForm):
class Meta:
model = Measurement
fields = ['value', 'measurement_date']
views.py
def new_measurement(request):
if request.method == 'POST':
form_SP = MeassurementSystolicPressureForm(request.POST or None)
form_DP = MeassurementDiastolicPressureForm(request.POST or None)
if form_CS.is_valid() or form_CR.is_valid():
temp_S = form_SP.save(commit=False)
temp_S.username = request.user
temp_S.measurement_type = 'syspres'
temp_S.save()
temp_D = form_DP.save(commit=False)
temp_D.username = request.user
temp_D.measurement_type = 'diapres'
temp_D.save()
return redirect('/')
else:
form_SP = MeassurementSystolicPressureForm()
form_DP = MeassurementDiastolicPressureForm()
args = {'form_SP': form_SP, 'form_DP': form_DP}
return render(request, 'measurements.html', args)
If for example I submit data for:
Systolic Pressure:
value: 120
date: 2019-01-15 16:15:32
Diastolic Pressure:
value: 80
date: 2019-01-15 15:00:00`
In my database I have two records:
username: Julka, measurement_type:
syspres, value: 80, date: 2019-01-15 15:00:00
username: Julka, measurement_type: diapres, value: 80, date: 2019-01-15 15:00:00
I have no idea what to do.
In an HttpRequest object, the GET and POST attributes are instances of django.http.QueryDict. This type alone cannot determine which form was submitted. Your forms have the same fields, so then one form is valid, other form valid too. That's why you have measurement_date and value are the same for both records. To solve this problem, you can add additional hidden fields to your forms and look at them from which form was sent. Some like this:
class MeassurementSystolicPressureForm(ModelForm):
flag_Systolic = forms.IntegerField()
class Meta:
model = Measurement
fields = ['value', 'measurement_date']
def __init__(self, *args, **kwargs):
super(MeassurementSystolicPressureForm, self).__init__(*args, **kwargs)
self.fields['flag_Systolic'].widget = forms.HiddenInput()
class MeassurementDiastolicPressureForm(ModelForm):
flag_Diastolic = forms.IntegerField()
class Meta:
model = Measurement
fields = ['value', 'measurement_date']
def __init__(self, *args, **kwargs):
super(MeassurementDiastolicPressureForm, self).__init__(*args, **kwargs)
self.fields['flag_Diastolic'].widget = forms.HiddenInput()
and in your views:
def new_measurement(request):
if request.method == 'POST':
if 'flag_Systolic' in request.POST:
form_SP = MeassurementSystolicPressureForm(request.POST)
if form_SP.is_valid():
temp_S = form_SP.save(commit=False)
temp_S.username = request.user
temp_S.measurement_type = 'syspres'
temp_S.save()
return redirect('/')
elif 'flag_Diastolic' in request.POST:
form_DP = MeassurementDiastolicPressureForm(request.POST)
if form_DP.is_valid():
temp_D = form_DP.save(commit=False)
temp_D.username = request.user
temp_D.measurement_type = 'diapres'
temp_D.save()
return redirect('/')
else:
form_SP = MeassurementSystolicPressureForm()
form_DP = MeassurementDiastolicPressureForm()
args = {'form_SP': form_SP, 'form_DP': form_DP}
return render(request, 'measurements.html', args)
I know maybe it is too late but it might be helpful for other people facing the same problem.
One easier solution would be creating the object in the View and passing it to both forms:
from .models import Measurement
def new_measurement(request):
user=request.user #the authenticated user
if request.method == 'POST':
measurement=Measurement(username=user)
form_SP = MeassurementSystolicPressureForm(request.POST or None, instance=measurement)
form_DP = MeassurementDiastolicPressureForm(request.POST or None, instance=measurement)
if form_CS.is_valid() or form_CR.is_valid():
form_CS.save()
form_CR.save()
return redirect('/')
else:
form_SP = MeassurementSystolicPressureForm()
form_DP = MeassurementDiastolicPressureForm()
args = {'form_SP': form_SP, 'form_DP': form_DP}
return render(request, 'measurements.html', args)
views.py
def patient_num(request):
if request.method == 'POST':
form = EditToBeSaveForm(request.POST)
if form.is_valid():
num = form.cleaned_data['病人编号']
new_p = Patient.objects.get(p_number=num)
if new_p:
new_p.p_name = form.cleaned_data['姓名']
new_p.p_sex = form.cleaned_data['性别']
new_p.p_age = form.cleaned_data['年龄']
new_p.p_tel_number = form.cleaned_data['电话号码']
new_p.save()
return render(request, 'polls/patient_edit.html')
else:
form = EditToBeSaveForm()
return render(request, 'polls/patient_num.html', {'form': form})
models.py
class Patient(models.Model):
sex_choice = (
('男', '男'),
('女', '女'),
)
p_name = models.CharField(max_length=100, default='template')
p_age = models.IntegerField(default=0)
p_number = models.IntegerField(default=0)
p_tel_number = models.IntegerField(default=0)
p_sex = models.CharField(choices=sex_choice, max_length=2, default='男')
forms.py
class EditForm(forms.Form):
病人编号 = forms.IntegerField()
class EditToBeSaveForm(forms.Form):
sex_choice = (
('male', '男'),
('female', '女'),
)
病人编号 = forms.IntegerField(label='你要修改的病人编号')
姓名 = forms.CharField(max_length=100)
年龄 = forms.IntegerField()
电话号码 = forms.IntegerField()
性别 = forms.ChoiceField(choices=sex_choice)
after i populate the form and submit it, the view didn't update the database instance,why?
i can do it one by one in shell as below.
new confuse!when i populate the form with invalid value,for example, an inexistent id of Patient object,it will still render the template,why?
It seems to me your problem is that you never reach the code under the if form.is_valid() of your patient_num view. Try to add some prints after the if form.is_valid() clause and make sure your form is valid. It is expected that your model will not be updated if your form is not valid.
Your problem here that you are passing request to form instead request.POST
form = EditToBeSaveForm(request.POST)
i put some 'print stuff' in my view and disvocer sth:
def patient_num(request):
print(111)
if request.method == 'POST':
print(2222)
form = EditToBeSaveForm(request.POST)
if form.is_valid():
print(3333)
num = form.cleaned_data['病人编号']
new_p = Patient.objects.get(p_number=num)
if new_p:
print(4444)
new_p.p_name = form.cleaned_data['姓名']
new_p.p_sex = form.cleaned_data['性别']
new_p.p_age = form.cleaned_data['年龄']
new_p.p_tel_number = form.cleaned_data['电话号码']
new_p.save()
return render(request, 'polls/patient_edit.html')
else:
form = EditToBeSaveForm()
return render(request, 'polls/patient_num.html', {'form': form})
i can only see 111 in the shell output.it seems that the view even didn't receive the post request.and i check my html file and find the problem.the form's destination direct to another view function…… it's so stupid, i'm sorry for waste your time !
As commented below, I will try to explain my problem more precisely with more code examples
In my application I am using a model inheritance, where base model class is:
class Entity(models.Model):
VISIBILITY_LEVELS = (
('0', 'Private'),
('1', 'Closedy'),
('2', 'Public'),
)
entityType = models.CharField(max_length=50)
userCreatedBy = models.ForeignKey(User, related_name='user_createdby', editable=False)
dateCreated = models.DateField(editable=False)
lastModified = models.DateField()
lastModifiedBy = models.ForeignKey(User, related_name='user_lastModifiedBy')
tags = models.ManyToManyField('Tag', blank=True, null=True)
visibilityLevel = models.CharField(max_length=1, choices=VISIBILITY_LEVELS, default=False)
In my form, I am editing a model which derived from Entity:
class Place(Entity):
name = models.CharField(max_length=155)
description = models.TextField()
location = models.ForeignKey(Point)
And the point and tag models are:
class Point(models.Model):
lat = models.FloatField() #coordinates
lng = models.FloatField()
hgt = models.FloatField(default=0.0)
class Tag(models.Model):
tagName = models.CharField(max_length=250) #tag name
usedAmount = models.IntegerField(default = 1) #how many entities has this tag
All of the models have primary key generated automatically by django. In my site, I am using AJAX to process the form (currently, there is no AJAX validation, but it will be ;)
My first problem was: what will be the easiest way for creating a form for adding a new Place object? Where the most tricky part was Tag addition, cause I need to enable both - adding new tags and selecting existing tags. I am new to Django, so some my attempts can be naive.. My solution was to make two Forms inherited from ModelForm for Point and Place and one custom Form for Tag. In tag form, I want to enable user to choose existing tags from DB or to type new tags separated by ';' in a textinput. So I have created those 3 forms:
class PlaceAddForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PlaceAddForm, self).__init__(*args, **kwargs)
self.fields['name'].label = "Name"
self.fields['description'].label = "Desc"
self.fields['visibilityLevel'].label = "Visibility"
class Meta:
model = Place
fields = ('name', 'description',
'visibilityLevel' )
#--
class PointForm(forms.ModelForm):
class Meta:
model = Point
#---
class TagAppendForm(forms.Form):
newTags = forms.CharField(
widget=forms.TextInput(),
required = False,
)
tagsAll = forms.ModelMultipleChoiceField(
label="Choose:",
queryset=Tag.objects.all(),
required = False
)
def clean_newTags(self):
if len(self.cleaned_data['newTags']) == 0:
return []
tags = self.cleaned_data['newTags'].split(';')
for t in tags:
if len(t) == 0:
raise forms.ValidationError('Tag must be minum 3 characters long')
return tags
And then in my template I have:
<form id="save-form" method="post" action="/user/places/addedit/">
{{ pointForm.as_p }}
{{ placeForm.as_p }}
{{ tagForm.as_p }}
<input type="submit" value="save" />
</form>
And View is:
def place_save_view(request):
ajax = request.GET.has_key('ajax')
if request.method == 'POST':
placeForm = PlaceAddForm(request.POST)
pointForm = PointForm(request.POST)
tagForm = TagAppendForm(request.POST)
if placeForm.is_valid() and tagForm.is_valid() and pointForm.is_valid():
place = _place_save(request, placeForm, pointForm, tagForm)
variables = RequestContext(request, {'places' : place })
return HttpResponseRedirect('/user/places/', variables)
#else:
# if ajax:
elif request.GET.has_key('entity_ptr_id'):
place = Place.objects.get(id==request.GET['entity_ptr_id'])
placeForm = PlaceAddForm(request.GET,instance=place)
point = place.location
pointForm = PointForm(request.GET,instance=point)
tagForm = TagAppendForm(initial={'tagsAll': place.tags.values_list('id', flat=True)})
else:
placeForm = PlaceAddForm()
pointForm = PointForm()
tagForm = TagAppendForm()
variables = RequestContext(request, {'placeForm': placeForm, 'pointForm': pointForm, 'tagForm': tagForm })
if ajax:
return render_to_response('places/place_save.html', variables)
else:
return render_to_response('places/add-edit-place.html', variables)
And finally AJAX:
function placeAdd() {
var div = $(document).find("#leftcontent");
div.load("/user/places/addedit/?ajax", null, function() {
$("#save-form").submit(placeSave);
});
return false;
}
function placeSave()
{
var item = $(this).parent();
var data = {
lng: item.find("#id_lng").val(),
lat: item.find("#id_lat").val(),
hgt: item.find("#id_hgt").val(),
name: item.find("#id_name").val(),
description: item.find("#id_description").val(),
pType: item.find("#id_pType").val(),
visibilityLevel: item.find("#id_visibilityLevel").val(),
newTags: item.find("#id_newTags").val(),
tagsAll: item.find("#id_tagsAll").val()
};
$.post("/user/places/addedit/?ajax", data, function(result){
if(result != "failure")
{
//todo....
}
else {
alert("Failure!");
}
});
return false;
}
Here I have more than a topic question to ask:
Is a better solution for doing that? (ok, I can create a one custom form but I loose some automation parts...)
Why after submitting form with blank newTags and no selection on tagsAll fields, I have an error for the tagsAll field (it appears on form, above tagsAll field):
"null" is not a valid value for a primary key
I have also a problem with displaying errors on form, when AJAX is used... :/ I find solution, that I must manually iterate through them on template with form (http://infinite-sushi.com/2011/06/using-ajax-with-django/).. For any other solution I will be grateful :)
Is it possible for an inlineformset_factory to take in a ModelForm as well as a model. When I try to run this I get an error message 'NoneType' object is not iterable.
Please help, I've spent an entire day trying to figure this out. Thanks.
Code:
Model.py
class FilterForm(ModelForm):
firstFilter = forms.BooleanField(label='First Filter', initial=False, required=False)
class Meta:
model = Filter
exclude = ('order')
class Controller(models.Model):
protocol = models.CharField('Protocol',max_length=64, choices=PROTOCOLS, default='http')
server = models.CharField('Server', max_length=64, choices=SERVERS, default='127.0.0.1')
name = models.CharField('Name', max_length=64)
def __unicode__(self):
return self.protocol + '://' + self.server + '/' + self.name
view.py
def controller_details(request, object_id):
controller = Controller.objects.get(pk=object_id)
controllerURI = controller.protocol + '://' + controller.server + '/' + controller.name
FilterFormSet = inlineformset_factory(Controller, FilterForm, extra=5)
if request.method == 'POST':
formset = FilterFormSet(request.POST, request.FILES, instance=controller)
if formset.is_valid():
filters = []
# Save all the filters into a list
forms = formset.cleaned_data
for form in forms:
if form:
protocol = form['protocol']
server = form['server']
name = form['name']
targetURI = form['targetURI']
filterType = form['filterType']
firstFilter = form['firstFilter']
if firstFilter == True:
aFilter = Filter(controller=controller, protocol=protocol, server=server, name=name, targetURI=targetURI, filterType=filterType, order=0)
else:
aFilter = Filter(controller=controller, protocol=protocol, server=server, name=name, targetURI=targetURI, filterType=filterType, order=-1)
filters.append(aFilter)
# Find the first filter in the list of filters
for index, aFilter in enumerate(filters):
if aFilter.order == 0:
break
if filters[index].targetURI:
test = "yes"
else:
for aFilter in filters:
aFilter.save()
else:
formset = FilterFormSet(instance=controller)
return render_to_response('controller_details.html', {'formset':formset, 'controllerURI':controllerURI}, context_instance=RequestContext(request))
UPDATE: If you intended to create a FormSet with Controller and Filter models where Filter holds a FK to the Controller, you need:
FilterFormSet = inlineformset_factory(Controller, Filter, form=FilterForm)
Note that in your code above, you're only passing the the Controller model class, which caused some confusion.