I have this model:
class Order(models.Model):
client = models.ForeignKey(
Client,
on_delete=models.CASCADE,
related_name='orders',
related_query_name='order',
)
delivery_country = CountryField('Kraj wysyłki', null=True, blank=True)
delivery_city = models.CharField('Miasto wysyłki', max_length=255, null=True, blank=True)
delivery_street = models.CharField('Ulica wysyłki', max_length=255, null=True, blank=True)
billing_country = CountryField('Kraj dla faktury', null=True, blank=True)
billing_city = models.CharField('Miasto dla faktury', max_length=255, null=True, blank=True)
billing_street = models.CharField('Ulica dla faktury', max_length=255, null=True, blank=True)
products = models.ManyToManyField(Product, through='OrderProduct')
class Client(models.Model):
name = models.CharField('Imię', max_length=255)
surname = models.CharField('Nazwisko', max_length=255)
country = CountryField('Kraj', null=True, blank=True)
city = models.CharField('Miasto', max_length=255, null=True, blank=True)
street = models.CharField('Ulica', max_length=255, null=True, blank=True)
And I would like to achieve this behaviour in Django Admin: After every change of Order.client
I want to prepopulate other fields like this:
Order.delivery_country = client.country
Order.delivery_city = client.city
Order.delivery_street = client.street
Order.billing_country = client.country
Order.billing_city = client.city
Order.billing_street = client.street
Is there any package/plugin to achieve this? Or maybe you have some ideas how I could do it myself?
Ok I achieved this:
(function ($) {
$(document).ready(function(){
$("select[name='client']").on("change", function(){
$.ajax({
url: "/admin/clients/"+$(this).val(),
type: "GET",
}).done(function(data){
client_data = $.parseJSON(data);
client_data = client_data[0];
$("select[name='delivery_country']").val(client_data.fields.country);
$("input[name='delivery_city']").val(client_data.fields.city);
$("input[name='delivery_street']").val(client_data.fields.street);
$("select[name='billing_country']").val(client_data.fields.country);
$("input[name='billing_city']").val(client_data.fields.city);
$("input[name='billing_street']").val(client_data.fields.street);
});
});
});
})(django.jQuery);
def get_client_ajax(request, id):
return JsonResponse(serializers.serialize('json', Client.objects.filter(id=id)), safe=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.