Some advanced methods in django-admin and manage.py

30 篇文章 0 订阅

Q1: Do you know differences between the two in title?

manage.py does the same thing as django-admin but takes care of a few things for you:

It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.

Q2. When you do not want to migrate a Model Class, what should you do?

add Meta class to you Model Class, eg:

class SomeCls(models.Model):
    item = models.CharField(primary_key=True, max_length=255)
    date = models.DateTimeField(blank=True, null=True)
    description = models.TextField(blank=True, null=True)

    class Meta:
        managed = False# See this!!
        db_table = 'siri_date_common'

Follwing are some advanced methods you should know

django-admin dbshell (python manage.py dbshell)

Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings.
  • For PostgreSQL, this runs the psql command-line client.
  • For MySQL, this runs the mysql command-line client.
  • For SQLite, this runs the sqlite3 command-line client.
  • For Oracle, this runs the sqlplus command-line client.

    This command assumes the programs are on your PATH so that a simple call to the program name (psql, mysql, sqlite3, sqlplus) will find the program in the right place. There’s no way to specify the location of the program manually.

django-admin diffsettings

Displays differences between the current settings file and Django’s default settings (or another settings file specified by --default).

dumpdata

django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]

python manage.py  dumpdata halo.RunoobTbl >out.json
django-admin dumpdata --exclude=auth --exclude=contenttypes

django-admin flush

Removes all data from the database and re-executes any post-synchronization handlers. The table of which migrations have been applied is not cleared.

If you would rather start from an empty database and re-run all migrations, you should drop and recreate the database and then run migrate instead.

inspectdb

django-admin inspectdb [table [table ...]]

Introspects the database tables in the database pointed-to by the NAME setting and outputs a Django model module (a models.py file) to standard output. You may choose what tables to inspect by passing their names as arguments.

Use this if you have a legacy database with which you’d like to use Django. The script will inspect the database and create a model for each table within it.

这里写图片描述

loaddata(match dumpdata command)

django-admin loaddata fixture [fixture ...]
Searches for and loads the contents of the named fixture into the database.

What’s a “fixture”?¶
A fixture is a collection of files that contain the serialized contents of the database. Each fixture has a unique name, and the files that comprise the fixture can be distributed over multiple directories, in multiple applications.

Django will search in three locations for fixtures:

In the fixtures directory of every installed application
In any directory named in the FIXTURE_DIRS setting
In the literal path named by the fixture
Django will load any and all fixtures it finds in these locations that match the provided fixture names.

If the named fixture has a file extension, only fixtures of that type will be loaded. For example:

django-admin loaddata mydata.json
would only load JSON fixtures called mydata. The fixture extension must correspond to the registered name of a serializer (e.g., json or xml).

If you omit the extensions, Django will search all available fixture types for a matching fixture. For example:

django-admin loaddata mydata
would look for any fixture of any fixture type called mydata. If a fixture directory contained mydata.json, that fixture would be loaded as a JSON fixture.

Compressed fixtures

Fixtures may be compressed in zip, gz, or bz2 format. For example:

django-admin loaddata mydata.json
would look for any of mydata.json, mydata.json.zip, mydata.json.gz, or mydata.json.bz2. The first file contained within a zip-compressed archive is used.

Note that if two fixtures with the same name but different fixture type are discovered (for example, if mydata.json and mydata.xml.gz were found in the same fixture directory), fixture installation will be aborted, and any data installed in the call to loaddata will be removed from the database.

makemigrations

django-admin makemigrations [app_label [app_label ...]]
Creates new migrations based on the changes detected to your models. Migrations, their relationship with apps and more are covered in depth in the migrations documentation.

Providing one or more app names as arguments will limit the migrations created to the app(s) specified and any dependencies needed (the table at the other end of a ForeignKey, for example).

migrate

django-admin migrate [app_label] [migration_name]
Synchronizes the database state with the current set of models and migrations.

runserver

django-admin runserver [addrport]
Starts a lightweight development Web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.

For more details please look up in django-admin in official doc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值