you are absolutely right and that solution is done before many days. but requirement is this only that i have explained earlier to you i want your guidance in achieving that
like the saved value of one model i select in other model then the same value of other model is should be filled with the selected value of the previous model and at the same moment only
for eg.
detail model
name: abc
email: abc@gmail.com
it is saved now with name abc(self.name method pass)
task model
detail(foreignkey): abc(i selected)
name:
email:
this 2 field should be filled automatically on the basis of the selected detail model value
Based on the link Ken gave you above, you should be able to add a javascript file to your admin page. This javascript would do the following:
- on page load, add an handler on the change event of the Detail selector (inspect the admin page in your browser to get the id of this field)
- on change, request a view (to be created) in your app with the id of the selected detail, that should return the information of this Detail (e.g. a Json containing the
name
and email
of the Detail)
- use the returned name and email to fill the value of the Task name and email (inspect the page in browser to get those field ids)
- handle the specific case of unselecting a Detail (should reset - empty - the Task name and email fields)
<!-- Add this to your template, assuming you're using the Django admin interface -->
<script>
document.addEventListener("DOMContentLoaded", function() {
// Replace these IDs with the actual IDs from your HTML
var detailSelector = document.getElementById("id_detail"); // ID of the Detail selector
var nameInput = document.getElementById("id_name"); // ID of the Task name input
var emailInput = document.getElementById("id_email"); // ID of the Task email input
detailSelector.addEventListener("change", function() {
var selectedDetailId = detailSelector.value;
if (selectedDetailId) {
// Make an AJAX request to the Django view
var xhr = new XMLHttpRequest();
xhr.open("GET", "/get_detail_info/" + selectedDetailId + "/");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
// Parse the JSON response
var response = JSON.parse(xhr.responseText);
// Update the Task name and email inputs
nameInput.value = response.name;
emailInput.value = response.email;
} else {
console.error("Error fetching detail information");
}
}
};
xhr.send();
} else {
// Handle the case when no Detail is selected (reset the Task name and email)
nameInput.value = "";
emailInput.value = "";
}
});
});
</script>
js code
the solution is done it is just need to make an api between those 2 then need to fetch the details. Then make a html file need to create js and inside that ajax and also the api fetching is should be inside the script by fetch() command.
and then in views file just add class for methods to call that the values and fetch it for tat use queryset and connect serializers to that particular models after that for getting value convert object data to dictionary by dict then convert dict to json formatted strings by json.dumps() and then return that with HttpResponse.