Tal*_*med 4 python django command-line python-3.x
我正在尝试从控制台创建一个对象,但不确定如何设置它。这是我的模型管理器:
class MajorManager(models.Manager):
def __str__(self):
return self.name
def createMajor(self, name):
try:
name = name.lower()
major = self.create(name=name)
except IntegrityError:
print("This major has already been created")
Run Code Online (Sandbox Code Playgroud)
这是模型:
class Majors(models.Model):
name = models.CharField(max_length=30, unique=True)
objects = MajorManager()
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
您可以使用 Django 的 API 走这条路线 -查看文档
\n首先创建一个外壳:
\npython manage.py shell
然后您可以导入模型并CRUD对其进行基础操作。
>>> from polls.models import Choice, Question # Import the model classes we just wrote.\n\n# No questions are in the system yet.\n>>> Question.objects.all()\n<QuerySet []>\n\n# Create a new Question.\n# Support for time zones is enabled in the default settings file, so\n# Django expects a datetime with tzinfo for pub_date. Use timezone.now()\n# instead of datetime.datetime.now() and it will do the right thing.\n>>> from django.utils import timezone\n>>> q = Question(question_text="What\'s new?", pub_date=timezone.now())\n\n# Save the object into the database. You have to call save() explicitly.\n>>> q.save()\nRun Code Online (Sandbox Code Playgroud)\n或者,您也可以尝试该dbshell路线,这里是文档。
\n\n此命令假定程序位于您的 PATH 上,因此对程序名称(psql、mysql、sqlite3、sqlplus)的简单调用将在正确的位置找到该程序。无法手动指定程序的位置。
\n
不过你不能使用 Django 的 ORM,它是纯 SQL,所以它的指令如下:
\nCREATE TABLE user (\n Id Int,\n Name Varchar\n);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
14485 次 |
| 最近记录: |