Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango models.py importing with no fields
    primarykey
    data
    text
    <p>As I am working through <a href="https://docs.djangoproject.com/en/1.4/topics/db/models/" rel="nofollow">https://docs.djangoproject.com/en/1.4/topics/db/models/</a> , in django, I have started a new app called <code>myapp</code> and edited the the <code>models.py</code> file as the walkthrough defines: </p> <pre><code>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) </code></pre> <p>Then I ran <code>manage.py syncdb</code> and tried to run the next few lines in the <code>manage.py shell</code> interactive prompt</p> <pre><code>&gt;&gt;&gt; p = Person(name="Fred Flintstone", gender="M") &gt;&gt;&gt; p.save() Traceback (most recent call last): .…[I removed a few lines]... DatabaseError: table myapp_person has no column named name &gt;&gt;&gt; p.gender u'M' &gt;&gt;&gt; p.get_gender_display() u'Male' </code></pre> <p>...so then I checked to see if sqlite3 had an object in the database called Person with the name column</p> <pre><code>C:\Users\djangoSite&gt;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 </code></pre> <p>);</p> <p>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 <code>dir(Person)</code> calls and it confirms that the name and gender fields aren't in there… secondly, I did <code>dir(p)</code> and it does have them. I suppose that is because we just added them in with that tuple, so that's not a surprise.</p> <p>The big questions then are:</p> <ol> <li>Why can't I 'p.save()'?</li> <li>Why doesn't the import call bring in objects with the fields that I designed in <code>models.py</code>?</li> <li>Is this by design?</li> <li>Am I just doing it wrong?</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload