# How can I access product, source and destination name instead of their "pk" in table

**URL:** <https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294>\
**Category:** Django Internals\
**Created:** [April 21, 2022, 11:22am UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294 "2022-04-21T11:22:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bibalani](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bibalani/32/8165_2.png) [@bibalani](https://forum.djangoproject.com/u/bibalani)\
**Post date:** [April 21, 2022, 11:23am UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/1 "2022-04-21T11:23:00Z")

</div>

Hi,  
How can I access product, source and destination name instead of their “pk” in table. below code is working. In fact ser\_order is below:  
`[{"model": "my_app.order", "pk": 220, "fields": {"sequence": 1172, "product": 1, "order_date": "2022-04-30", "source": 3, "destination": 4, "quantity": 23}}`]  
However, I need to have their name instead of their primary key for product, source and destination.  
Moreover, it try `ser_order = {'id':order.id,'sequence':order.sequence,'product':order.product,'quantity':order.quantity, 'order_date':order.order_date,'source':order.source, 'destination':order.destination}` but it does not work.

```auto
                order_instance = order_form.save(commit=True)
                ser_order = serializers.serialize('json',[order_instance,])
               return JsonResponse({'ser_order':ser_order}, status=200)

```

```auto
$(document).ready(function(){

        // $('#current_order_info').DataTable();

        $('#order_form').submit(function(e){
            e.preventDefault();
            var serializedData = $(this).serialize();
            var url = document.querySelector('#order_form').dataset.url
            // var url = "{% url 'my_app:add_order_regular_customer' %}"
            $.ajax({
                type: 'POST',
                url: url,
                data: serializedData,
                success: function(response){
                    $('#order_form').trigger('reset');
                    var ser_order = JSON.parse(response['ser_order']);
                    var fields = ser_order[0];

                    $('#current_order_info').append(
                        `<tr>
                        <td>${fields["pk"]||""}</td>
                        <td>${fields['fields']["sequence"]||""}</td>
                        <td>${fields['fields']["product"]||""}</td>
                        <td>${fields['fields']["quantity"]||""}</td>
                        <td>${fields['fields']["order_date"]||""}</td>
                        <td>${fields['fields']["source"]||""}</td>
                        <td>${fields['fields']["destination"]||""}</td>
                        </tr>`
                    )
               
                    $('#current_order_info').DataTable();
                },
              
            });
        });
    });

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 21, 2022, 11:37am UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/2 "2022-04-21T11:37:01Z")

</div>

> [@bibalani](#):
>
> ```auto
> order_instance = order_form.save(commit=True)
> ser_order = serializers.serialize('json',[order_instance,])
> return JsonResponse({'ser_order':ser_order}, status=200)
> 
> ```

I’m guessing this is part of the view you’re using?

> [@bibalani](#):
>
> Moreover, it try `ser_order = {'id':order.id,'sequence':order.sequence,'product':order.product,'quantity':order.quantity, 'order_date':order.order_date,'source':order.source, 'destination':order.destination}`  
> but it does not work.

You would need to be more specific here. How does it not work? What specifically did you try?

---

<div class="post-metadata">

**Author:** ![bibalani](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bibalani/32/8165_2.png) [@bibalani](https://forum.djangoproject.com/u/bibalani)\
**Post date:** [April 21, 2022, 11:54am UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/3 "2022-04-21T11:54:12Z")

</div>

I got this error:  
TypeError: Object of type Product is not JSON serializable

models.py

```auto
class Order(models.Model):
    sequence = models.IntegerField()
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    order_date = models.DateField()
    source = models.ForeignKey(UserProfileInfo, on_delete=models.CASCADE, related_name='order_source')
    destination = models.ForeignKey(UserProfileInfo, on_delete=models.CASCADE, related_name='order_destination')
    quantity = models.IntegerField()

```

views.py

```auto
            order_form = OrderForm(data=request.POST)
            if order_form.is_valid():
                order = order_form.save(commit=True)
                
ser_order = {'id':order.id,'sequence':order.sequence,'product':order.product,'quantity':order.quantity, 'order_date':order.order_date,'source':order.source, 'destination':order.destination}
return JsonResponse({'ser_order':ser_order}, status=200)

```

regular\_customer.html

```auto
    $(document).ready(function(){
        // $('#current_order_info').DataTable();
        $('#order_form').submit(function(e){
            e.preventDefault();
            
            var serializedData = $(this).serialize();
            var url = document.querySelector('#order_form').dataset.url
            // var url = "{% url 'my_app:add_order_regular_customer' %}"
            $.ajax({
                type: 'POST',
                url: url,
                data: serializedData,
                success: function(response){
                    $('#order_form').trigger('reset');
                    var ser_order = JSON.parse(response['ser_order']);
                    var fields = ser_order[0];

                    var sequence_value_fields = response['ser_sequence_value']['sequence_value'];
                    $("#id_sequence").val(sequence_value_fields)

                    
                    $('#current_order_info').append(
                        `<tr>
                        <td>${fields["pk"]||""}</td>
                        <td>${fields['fields']["sequence"]||""}</td>
                        <td>${fields['fields']["product"]||""}</td>
                        <td>${fields['fields']["quantity"]||""}</td>
                        <td>${fields['fields']["order_date"]||""}</td>
                        <td>${fields['fields']["source"]||""}</td>
                        <td>${fields['fields']["destination"]||""}</td>
                        </tr>`
                    ) 
                       
                    $('#current_order_info').DataTable();

                },
                
                         
            });
            
            
        });
        
    });

    

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 21, 2022, 12:03pm UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/4 "2022-04-21T12:03:21Z")

</div>

> [@bibalani](#):
>
> I got this error:  
> TypeError: Object of type Product is not JSON serializable

Do you want the entire Product instance as part of your JSON or just an individual field?

---

<div class="post-metadata">

**Author:** ![bibalani](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bibalani/32/8165_2.png) [@bibalani](https://forum.djangoproject.com/u/bibalani)\
**Post date:** [April 21, 2022, 12:05pm UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/5 "2022-04-21T12:05:47Z")

</div>

I need to have product name in the datatable  
models.py:

```auto
class Product(models.Model):
    name = models.CharField(max_length=20)
    produced_date = models.DateField(blank=True)
    expired_date = models.DateField(blank=True)
    company = models.CharField(max_length=20, blank=True)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 21, 2022, 12:07pm UTC](https://forum.djangoproject.com/t/how-can-i-access-product-source-and-destination-name-instead-of-their-pk-in-table/13294/6 "2022-04-21T12:07:03Z")

</div>

Ok, so _that’s_ the field you want to specify in your JSON.
