Dr.Gonzo
Dr.Gonzo

Reputation: 107

Querying Binary data using Django and PostgreSQL

I am trying to print out to the console the actual content value (which is html) of the field 'htmlfile': 16543. (See below)

So far I am able to print out the whole row using .values() method

Here is what I am getting in my python shell:

>>>
>>> Htmlfiles.objects.values()[0]
{'id': 1, 'name': 'error.html', 'htmlfile': 16543}
>>>

I want to print out the content of 16543.. I have scanned through the Django QuerySet docs so many times and still cannot find the right method..

Here is my data model in models.py:

class Htmlfiles(models.Model):
    name = models.CharField(max_length=30, blank=True, null=True)
    htmlfile = models.TextField(blank=True, null=True)  

    class Meta:
        managed = False
        db_table = 'htmlfiles'

Any assistance would be greatly appreciated.

Upvotes: 2

Views: 634

Answers (1)

solarissmoke
solarissmoke

Reputation: 31414

You can fetch only the htmlfield value with:

Htmlfiles.objects.values('htmlfile')

Which, for each row, will give you an dictionary like so:

{'htmlfile': 12345}

So to print all the htmlfile values something like this is what you need:

objects = Htmlfiles.objects.values('htmlfile')
for obj in objects:
    print(obj['htmlfile'])

Upvotes: 2

Related Questions