As I am working through https://docs.djangoproject.com/en/1.4/topics/db/models/ , in django, I have started a new app called myapp
and edited the the models.py
file as the walkthrough defines:
class Person(models.Model):
GENDER_CHOICES = (
(u M , u Male ),
(u F , u Female )
)
name = models.CharField(max_length=60)
gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
然后,我打上了<代码>manage.py syncdb,并试图在<代码>manage.py Shell中操作下面几条线路。
>>> p = Person(name="Fred Flintstone", gender="M")
>>> p.save()
Traceback (most recent call last):
.…[I removed a few lines]...
DatabaseError: table myapp_person has no column named name
>>> p.gender
u M
>>> p.get_gender_display()
u Male
......因此,我检查了Sqlite3在名为名列人的数据库中是否有物体。
C:UsersdjangoSite>python manage.py sql myapp
BEGIN;
CREATE TABLE "myapp_person" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(60) NOT NULL,
"gender" varchar(2) NOT NULL
;
And of course it does! So the Person model exists, however it doesn t get the fields that I designed it with in models.py? I did a dir(Person)
calls and it confirms that the name and gender fields aren t in there… secondly, I did dir(p)
and it does have them. I suppose that is because we just added them in with that tuple, so that s not a surprise.
当时的主要问题是:
- Why can t I p.save() ?
- Why doesn t the import call bring in objects with the fields that I designed in
models.py
? - Is this by design?
- Am I just doing it wrong?