开发者

changing django default model settings

I'm just starting with the django creating your own app tutoria开发者_如何学编程l (creating a Poll) I'm deviating slightly as I'm wanting to create an app using my own database model that already exists.

And in the tutorial it says

  • Table names are automatically generated by combining the name of the app (polls) and the lowercase name of the model -- poll and choice. (You can override this behavior.)
  • Primary keys (IDs) are added automatically. (You can override this, too.)
  • By convention, Django appends "_id" to the foreign key field name. Yes, you can override this, as well.

But I can't see where it mentions how you can override this behaviour? I've defined my model as so

from django.db import models

# Create your models here.
class Channels(models.Model):
    channelid = models.IntegerField()
    channelid.primary_key = True
    channelname = models.CharField(max_length=50)

Now when I go in to the shell this is what I get

>>> from tvlistings.models import *
>>> Channels.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 67, in __
repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 82, in __
len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
    for row in compiler.results_iter():
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 35, in defau
lterrorhandler
    raise errorclass, errorvalue
DatabaseError: (1146, "Table 'tvlistings.tvlistings_channels' doesn't exist")

Obviously it can't find the table tvlistings_channels as it's actually called channels. So how do I change the default?


You can override Model behavior in Django largely through the use of an inner Meta class

  • db_table allows you to rename the table name
  • specifying another field as the primary key will have it use that rather than a surrogate key (not in the Meta class, just in the model itself)


You should try and work your way all through the tutorial before you try and customise things. All these things are covered in the actual documentation, but it's best to have a basic understanding of things first before diving into that.

FWIW, here are the docs on defining your own primary key and specifying a table name. But really, do the tutorial as written first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜