Lleims
Lleims

Reputation: 1353

Obtain all the columns from my model in django

I want to obtain all the info from one table of my database in django, I'm doing this,

from django.shortcuts import render
# Create your views here.
from Platform_App.models import Fleet

def list_fleets(request):
    fleet = Fleet.objects.all()
    return render(request, template_name='fleets.html', context={'Fleet':fleet})

But it shows an error in the line below, concretely in fleets.

fleet = Fleet.objects.all()

It says,

class fleets has no objects member

My model is the following:

from django.db import models

class Fleet(models.Model):
    fleet_id = models.IntegerField(primary_key=True)
    fleet_name = models.CharField(max_length=20)
    description = models.CharField(max_length=40, blank=True, null=True)

If fleet = Fleet.objects.all() is not correct, how I have to do it to take all the columns of my table??

Probably a easy question, but I'm newbie. Thank you very much!!!!

Upvotes: 1

Views: 69

Answers (1)

Srijwal R
Srijwal R

Reputation: 573

Hope this link and the below codes will help you:

models.py

from django.db import models

class Fleet(models.Model):
    fleet_id = models.IntegerField(primary_key=True)
    fleet_name = models.CharField(max_length=20)
    description = models.CharField(max_length=40, blank=True, null=True)

views.py

from django.shortcuts import render
# Create your views here.
from Platform_App.models import Fleet

def fleet_list(request):
    fleet = Fleet.objects.all()
    return render(request, template_name='fleets.html', context = {'fleets':fleet})

and in your template fleets.html

{% for fleet in fleets %}

FYI: Let me know if this worked.

Upvotes: 2

Related Questions