使用SQLite设置Django数据库访问API的问题

时间:2021-05-08 19:15:18

I just started using Django, and I am going through the documentation here to build my first app, but I am running into some kind of issue related to the database access API for SQLite.

我刚刚开始使用Django,我将通过这里的文档来构建我的第一个应用程序,但是我遇到了与SQLite的数据库访问API相关的某种问题。

My directory structure looks like this:

我的目录结构如下所示:

使用SQLite设置Django数据库访问API的问题

The only files I have edited are models.py and settings.py and it is all code from the documentation.

我编辑的唯一文件是models.py和settings.py,它是文档中的所有代码。

models.py:

models.py:

from django.db import models

class Question(models.Model):
    # ...
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text

class Choice(models.Model):
    # ...
    def __str__(self):              # __unicode__ on Python 2
        return self.choice_text

import datetime

from django.db import models
from django.utils import timezone


class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

The only changes I have made to settings.py are adding my timezone TIME_ZONE = 'US/Pacific' and adding 'polls', to INSTALLED_APPS.

我对settings.py所做的唯一更改是将我的时区TIME_ZONE ='US / Pacific'添加到INSTALLED_APPS并添加'polls'。

(For full disclosure, I have set up my urls.py just to test hello world, which is not part of the documentation, but I don't think that's causing the issue. Here's the code for that if it's relevant.)

(为了完全披露,我已经设置了我的urls.py只是为了测试hello world,这不是文档的一部分,但我认为这不会导致问题。如果相关的话,这是代码。)

urls.py:

urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.debug import default_urlconf
from django.http import HttpResponse

def hello(request):
    return HttpResponse('Hello world!!!')

urlpatterns = patterns('',
    # Examples:
    url(r'^$', hello),
    # url(r'^blog/', include('blog.urls')),

    #url(r'^admin/', include(admin.site.urls)),
    #url(r'^$', default_urlconf),
)

Now, the issue I'm running into is when I get to the "Playing with the API" section. When I open a python manage.py shell for the second time in that section, I'm supposed to be able to use the command Question.objects.all() and get the result [<Question: What's up?>] with "What's up?" being the value of question_text. The problem is I'm still getting the result [<Question: Question object>] instead of the question_text value.

现在,我遇到的问题是当我进入“使用API​​”部分时。当我在该部分第二次打开python manage.py shell时,我应该能够使用命令Question.objects.all()并获得结果[ <问题:怎么了?> ]这是怎么回事?”是question_text的价值。问题是我仍然得到结果[ ]而不是question_text值。 :question>

I've gone back and re-created my app three times in the hope that I missed something during the setup, but I get the same issue every time and I seem to be following the documentation exactly. Am I missing something here?

我已经回去并重新创建我的应用程序三次,希望我在设置过程中遗漏了一些东西,但我每次都得到同样的问题,我似乎完全遵循文档。我在这里错过了什么吗?

1 个解决方案

#1


1  

First, make sure that the contents of models.py match with what's given in the tutorial

首先,确保models.py的内容与教程中给出的内容相匹配

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):              # __str__ on Python 3
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200, default="")
    votes = models.IntegerField(default=0)

    def __unicode__(self):              # __str__ on Python 3
        return self.choice_text

Also make sure that the migrations are up to date.

还要确保迁移是最新的。

If you're running python 2, use __unicode__ else use __str__.

如果您正在运行python 2,请使用__unicode__否则使用__str__。

That should solve the problem for you.

那应该为你解决问题。

#1


1  

First, make sure that the contents of models.py match with what's given in the tutorial

首先,确保models.py的内容与教程中给出的内容相匹配

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):              # __str__ on Python 3
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200, default="")
    votes = models.IntegerField(default=0)

    def __unicode__(self):              # __str__ on Python 3
        return self.choice_text

Also make sure that the migrations are up to date.

还要确保迁移是最新的。

If you're running python 2, use __unicode__ else use __str__.

如果您正在运行python 2,请使用__unicode__否则使用__str__。

That should solve the problem for you.

那应该为你解决问题。