English 中文(简体)
Django模式。 进口无田
原标题:Django models.py importing with no fields

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.

当时的主要问题是:

  1. Why can t I p.save() ?
  2. Why doesn t the import call bring in objects with the fields that I designed in models.py?
  3. Is this by design?
  4. Am I just doing it wrong?
最佳回答

为什么能打上Ip.save(?

表格中没有一栏名。 <代码>manage.py sql myapp 仅凭你的模式,显示干jan的 generate。 没有实际表格。 (当然,它应当相应) 重要的是,你必须进入你的qlite数据库,核对表格及其图象。 我认为,在开发I syncdb时,稍后将对该模式进行修改。 Re-syncing db don treving the table. 您必须删除表格,然后再作改动。

为什么要把进口呼吁带入我设计成模型的领域。 ??

Please modify your question to reflect this issue, it doens t look like you have any issues accessing or instantiating a person object.

这是否通过设计?

我认为,这并不是设计的,因为新用户正在学习一个新框架,因此,这些都是共同的问题。 我在学到的数周和数月中都遇到了这些问题,这些问题将更加遥远。 保证你正确核实工作。 检查管理。 py sql myappn t that relevant. 检查贵国数据库以观察该表更为相关。 Django是一个成熟的平台,它给你一个错误,如<条码> 开放栏。 时间的99.9%完全意味着它是透明的。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签