How to populate a JqGrid in Django with data obtained from a query (queryset)

111 views
Skip to first unread message

João Rodrigues

unread,
Sep 25, 2016, 5:20:01 PM9/25/16
to Django users
I am working on a small online shop with Django 1.9 and JqGrid

Here is a simplification of my models:

    class Addresses(models.Model):
        address = models.TextField(db_column='ADDRESS', max_length=2000)
        city = models.CharField(db_column='CITY', max_length=400, blank=True, null=True)
        postal_code = models.CharField(db_column='POSTAL_CODE', max_length=200, blank=True, null=True)
        state = models.CharField(Country, db_column='STATE', max_length=400, blank=True, null=True)
        country = models.ForeignKey(Country, db_column='COUNTRY_ID', null=True)

    class Customers(models.Model):
        name = models.CharField(db_column='NAME', max_length=400)
        nif = models.CharField(db_column='NIF', max_length=200, null=True, blank=True)
        email = models.CharField(db_column='EMAIL', max_length=255, unique=True)
        mobile_number = models.CharField(db_column='MOBILE_NUMBER', max_length=200, blank=True, null=True)
        phone_number = models.CharField(db_column='PHONE_NUMBER', max_length=200, blank=True, null=True)
        addresses = models.ManyToManyField(Addresses, through='CustomerAddresses')


    class CustomerAddresses(models.Model):
        id = models.AutoField(db_column='ID', primary_key=True)
        address_name = models.CharField(db_column='ADDRESS_NAME', max_length=100)
        customer = models.ForeignKey(Customers)
        address = models.ForeignKey(Addresses)


    class Orders(models.Model):
        order_number = models.IntegerField(db_column='ORDER_NUMBER', unique=True)
        customer = models.ForeignKey('Customers', db_column='CUSTOMER_ID')
        payment_date = models.DateTimeField(db_column='PAYMENT_DATE', blank=True, null=True)
        payment_method = models.CharField(db_column='PAYMENT_METHOD', max_length=400, blank=True, null=True)
        delivery_address = models.ForeignKey('Addresses', db_column='ORDER_DELIVERY_ADDRESS_ID', related_name='delivery_address', blank=True, null=True)


I want an edit page for a given Customer to look like this:



At the bottom, I want to display grids (JqGrid) inside jQuery tabs.

The Orders grid, should contain all the orders that are associated with the current Customer

    queryset = Orders.objects.filter(customer=customer_pk)

Idem, for the Addresses.

As you can see, I successfully created the tabs and grid, BUT the grids are not filled with the required data (queryset).

Could you help me understand how I should populate the grids with the data I want?

I am sorry if this question is not well explained. I am new in Django. If there is something you don't understand, please tell me.

Thanks

Gergely Polonkai

unread,
Sep 26, 2016, 1:59:41 AM9/26/16
to Django users

Hello,

do you already know how to do it without Django, e.g with a static dataset? Because, from the HTML page's view, you will exactly do that.

I don't know JqGrid, but I assume it needs an array of objects, like

gridData = [
    {
        firstColumn: 1,
        secondColumn: "data"
    },
    {
        firstColumn: 2,
        secondColumn:" other data"
    }
];

Generate this array from your querysets inside a <script> block, and there you go!

It gets a bit trickier if you want to load it dynamically, like loading the next page of data without reloading the full page, but that's more the problem of AJAX, not Django.

Best,
Gergely


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7e9b341-f5e3-4010-bb63-78d4bec635ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

João Rodrigues

unread,
Sep 27, 2016, 7:26:38 AM9/27/16
to Django users
Thanks for your answer. The project is rather big and I am still not used to Django. I got some help and it has been solved.
Reply all
Reply to author
Forward
0 new messages