0

I would like to fetch data from a PostgreSQL using Django models/managers and use this data to render my frontend furthermore. I have the following plain simple example where I just get the defined privatekey value in return but not the other information from the table.

Estimated outcome:
I would like to get all elements of a single row as one object.

Models.py:

from django.db import models
import uuid

# Create your models here.
class AccountInformationManager(models.Manager):
    pass


class AccountInformation(models.Model):
    version = models.CharField(max_length=20, blank=False)
    DID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    accountNumber = models.IntegerField(blank=False)
    broker = models.CharField(max_length=50, blank=False)
    leverage = models.CharField(max_length=10, blank=False)
    account_balance = models.FloatField(max_length=15, blank=False)
    account_profit = models.FloatField(max_length=15, blank=False)
    account_equity = models.FloatField(max_length=15, blank=False)
    account_margin = models.FloatField(max_length=15, blank=False)
    account_margin_free = models.FloatField(max_length=15, blank=False)
    account_margin_level = models.FloatField(max_length=15, blank=False)
    account_currency = models.CharField(max_length=20, blank=False)

    objects = models.Manager()

    class Meta:
        db_table = 'AccountInfo'

Query.py:

from django.db import models
import sys
sys.path.insert(0, r"C:\Users\Jonas\Desktop\Dashex")
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'Dashex.settings'
import django
django.setup()
from Dashboard.models import AccountInformation

//// query data

account_information = AccountInformation.objects.all()
print(account_information)

Printed Output:

<QuerySet [<AccountInformation: AccountInformation object (30e61aec-0f6e-4fa0-8c1b-eb07f9347c1f)>, <AccountInformation: AccountInformation object (8b46c7c7-1bc8-4736-8dc5-7d5f012d594b)>]>

Process finished with exit code 0

Why it doesn't return the whole data from the table like broker, accountNumberetc.?

enter image description here

linked question:

Additionally, if I use AJAX + Javascript to render my frontend, would I then call the query.py script within my Ajax Url to fetch the data from the DB and render the frontend?

2
  • 3
    It does. You can access any of those fields on the returned queryset. Commented Dec 4, 2019 at 11:31
  • But no, in Django you don't call scripts individually. Please do the introductory tutorial which teaches about URLs and views. Commented Dec 4, 2019 at 11:32

2 Answers 2

1

What you see when you print(obj) is a representation of the object. By default, Django represents a Model object by the Model class name (AccountInformation), the word object to indicate that it's an instance and the pk of the object (the UUID in your case).

You can change how an object is printed by overriding the __str__() method on your Model. In fact, you should always do that.

The QuerySet you get is actually an iterable so you can iterate over the items in the result and each item will be the full AccountInformation instance, e.g.

for item in account_information:
    print(f"{item.DID}, version: {item.version}, number: {item.accountNumber} ...")

When you do an AJAX query, you should have a Django view to process the request and return a response to your client, as you would have for a normal browser page request. So no, your script is useful for testing purposes, but it should not be part of your app. Have you done the Django tutorial?

Sign up to request clarification or add additional context in comments.

Comments

1

In result it's showing queryset object. You can iterate over the queryset to get each row.

for row in queryset:
    print(row.broker, row.accountNumber)

For rendering to frontend, I will suggest to check the Django rest framework (DRF)
https://www.django-rest-framework.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.