Вопрос

Say we have the following class:

Class Alert(models.Model):
    contact = models.ForeignKey('emails.Contact',null=True,blank=True)

If I wanted to get the foreign key of the contact I would do somealert.contact.pk or somealert.contact_id. Do these commands pull down the whole contact object and then get the key? Or do any of them just yield the foreign key without pulling all off the attributes of the instance from the database. I worried about performance and would prefer to just get the key itself.

Это было полезно?

Решение

The first one - somealert.contact.pk - will get the Contact object. The second - somealert.contact_id - won't.

You can verify this in the shell by looking at the contents of django.db.connection.queries.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top