Hello, I read carefully through out the Django Docs and found one comment 
really misleading 
(https://docs.djangoproject.com/en/1.8/ref/models/fields/#foreignkey):

> Behind the scenes, Django appends "_id" to the field name to create its 
database column name. In the above example,
> the database table for the Car model will have a manufacturer_id column. 
(You can change this explicitly by specifying db_column 
<https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.Field.db_column>
) 
> However, your code should never have to deal with the database column 
name, unless you write custom SQL. 
> You’ll always deal with the field names of your model object.

Currently nothing in the first two paragraphs is obviously wrong, however 
the second part is. Since when dealing with SQL YOU SHOULD now your column 
name.
db_column Name could save you a really expensive join when you don't need 
to access more data.
I mean the best example obviously comes with Username's. Here is an example:

from django.db import models

class Tweet(models.Model):
    message = models.TextField()
   username = models.ForeignKey('auth.User', db_column='username')

When you now need to represent tweets to a user you could easily write that:
Tweet.objects.all()
You now have all the Tweets and the Usernames.

However if you want to present the Tweet and the Username while using the 
following:

from django.db import models

class Tweet(models.Model):
    message = models.TextField()
   user = models.ForeignKey('auth.User')

You would obviously need a join to get the username.

So that part is really misleading and It would be great to have more 
documentation about db_column since it could save some time at the database 
level, especially when dealing with UNIQUE strings etc.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/9f774f9e-b008-471a-a17f-8e3398c751c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to