2010年12月29日 星期三

[Django] Don't use field_id to named your field 別用_id命名資料庫爛位

class Car(models.Model):
    manufacturer = models.ForeignKey('production.Manufacturer')

Django 會自動加個 manufacturer_id 所以你不用自己去處理他
如果自己定義一個 xxx_id Django會confuse
除非你想自己處理SQL

官網原文

DATABASE REPRESENTATION
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) 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.


See Also
http://docs.djangoproject.com/en/dev/ref/models/fields/#database-representation
Related Posts Plugin for WordPress, Blogger...