0
votes

I'm creating a simple 'task manager' web application using Django 1.11 and ExtJS4 as a client side.

Each user created entry will have its own category. Now i have a problem - in the ExtJS grid, every category of an entry displaying as an 'id' value of category. And this is fair because i use Foreign key to id.

How can i display a type field instead of id in the ExtJS grid table?

I've tried to use django 'to_field = type' option and it's actually works, instead of 'category_id = id', i got a 'category_id = type'. But my teacher said that it's not a solution for this case and at backend part i should operate with an 'id'.

models.py

class Entry(models.Model):
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey('Category', on_delete=models.CASCADE)


class Category(models.Model):
    type = models.CharField(max_length=255, unique=True)

In views.py i'm sending JSON format for ExtJS client side

views.py

def JS_entries_list(request):
    list_of_entries = Entry.objects.all().values()
    context = {
        'data': list(list_of_entries)
    }
    return JsonResponse(context)

And here is my ExtJS model which i use for grid's store. Here, as you can see i receiving 'category_id' as an 'int'

Ext.define('Entry', {
        extend: 'Ext.data.Model',

        idProperty: 'entryID',

        fields: [{
            name: 'id',
            type: 'int',
        }, {
            name: 'uuid',
            type: 'string',
        }, {
            name: 'user_id',
            type: 'int',
        }, {
            name: 'headline',
            type: 'string',
        }, {
            name: 'body_text',
            type: 'string',
        }, {
            name: 'pub_date',
            type: 'date',
        }, {
            name: 'category_id',
            type: 'int',
        }, {
            name: 'fav_flag',
            type: 'bool',
        }, {
            name: 'published',
            type: 'bool',
        }]
    });

So if i get it right, now i should recieve an 'category id' from database, then somewhere (on the server or client side) convert it to the 'category type' and display in extjs grid. Now, can you, please, help me to find a correct and elegant way to do it?

1

1 Answers

0
votes

On your queryset you could specify the values you want from the database inside the .values() and then you'll have a list of objects. E.g list_of_entries = Entry.objects.all().values('headline', 'category__id', 'foo', 'bar')

Have a look here