I have model Employee and same table in my local database. I need to have the possibility to edit any record and save it locally. When I have something in the webflow_id field I got this error when I tried to select the edit option: Employee matching query does not exist.
When I tried to edit record without this webflow_id it doesn't change, but creates a new record.
my views.py:
def staff_edit(request, webflow_id):
#employees = Employee.objects.all()
#print(employees)
if request.method == 'GET':
if webflow_id == 0:
form = EmployeeEditForm()
else:
try:
#employees = Employee.objects.get(pk=webflow_id)
employees = Employee.objects.get(pk=webflow_id)
except Employee.DoesNotExist:
raise Http404("Employee DoesNotExist")
form = EmployeeEditForm(instance=employees)
return render(request, 'staffedit.html', {'form': form})
else:
if webflow_id == 0:
form = EmployeeEditForm(request.POST)
else:
employees = Employee.objects.get(pk=webflow_id)
form = EmployeeEditForm(request.POST, instance=employees)
if form.is_valid():
form.save()
return redirect('feedback:staff')
context = {'form': form} #when the form is invalid
return render(request, 'staffedit.html', context)
models.py:
class Employee(models.Model):
webflow_id = models.CharField(max_length=100, primary_key=True, default=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, default=True)
email = models.EmailField(max_length=100)
user_type = models.CharField(max_length=100)
status = models.CharField(max_length=100, default=True)
roles = models.ManyToManyField('Role', through='EmployeeRole')
def __str__(self):
return self.webflow_id + " " + self.email + " " + self.first_name + " " + self.last_name
this is my general html staff.html:
$(document).ready(function() {
var data;
fetch("http://192.168.2.85:8000/fetchapi_employees/")
.then(response => response.json())
.then(json => data = json)
.then(() => {console.log(data); //this gave me Array of objects from my database
$('#datatable').DataTable( {
data: data.employees,
deferRender: true,
scrollY: false,
scrollX: false,
scrollCollapse: true,
scroller: true,
"columns": [
{ data: "webflow_id" },
{ data: "first_name" },
{ data: "last_name" },
{ data: "email" },
{ render: function ( data, type, row ) {
return '<a href="http://192.168.2.85:8000/staff/edit/' + row.webflow_id + '"><i class="far fa-edit fa-lg" aria-hidden="true"></i></a>';
} },
{ render: function ( data, type, row ) {
return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
} },
],
"order": [[1, 'asc']]
} )
})
} );
staffedit.html:
<div class="container">
<div class="col-md-10.offset-md-1 mt-5">
<div class="jumbotron">
<h1 class="display-4">Edit employee</h1>
<hr class="my-4">
<form action="" method="post" autocomplete="off">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-success"><i class="far fa-save"></i> Save</button>
</form>
</div>
</div>
</div>
forms.py:
class EmployeeEditForm(ModelForm):
class Meta:
model = Employee
fields = [
'webflow_id',
'first_name',
'last_name',
'roles',
]
and this is my urls.py:
path('staff/edit/<int:webflow_id>', views.staff_edit, name="staffedit"),
Can anyone see what I'm missing?
Best regards!
question from:
https://stackoverflow.com/questions/65919683/error-message-employee-matching-query-does-not-exist