Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'model' object has no attribute 'id'

Tags:

python

django

I'm creating a project in Django and I'm having trouble, I'm trying to get the ID of objects in my database, but when I run it the object seems to not have an ID. Here is my model:

from __future__ import unicode_literals

from django.db import models

    class film(models.Model):

        filmName = models.CharField(primary_key=True, max_length=120)
        quotient = models.FloatField()
        rating = models.FloatField()
        gross = models.IntegerField()
        star = models.CharField(max_length=120)
        releaseDate = models.DateField()

        def __unicode__(self):
            return self.filmName

and in my view:

objects = film.objects.filter(filmName__startswith=letter)
for object in objects:
    print object.id

I get an error saying id does not exist and I'm not sure why

like image 730
user2320239 Avatar asked May 26 '26 11:05

user2320239


1 Answers

You've explicitly told Django that filmName is the primary key, so there is no separate ID.

It's not usually a good idea to do this, though.

like image 59
Daniel Roseman Avatar answered May 28 '26 00:05

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!