classAccessRecord(models.Model): name = models.ForeignKey(Webpage,on_delete=models.CASCADE) date = models.DateField(unique=True)
def__str__(self): return str(self.date)
3. 移植
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
root@38c80f8e29c3:/code# python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying sessions.0001_initial... OK
1 2 3 4 5 6 7
root@38c80f8e29c3:/code# python manage.py makemigrations first_app Migrations for 'first_app': first_app/migrations/0001_initial.py - Create model AccessRecord - Create model Topic - Create model Webpage - Add field name to accessrecord
1 2 3 4 5
root@38c80f8e29c3:/code# python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, first_app, sessions Running migrations: Applying first_app.0001_initial... OK
4. 在对应app的admin.py中注册admin将要管理的模块:
1 2 3 4 5 6
from django.contrib import admin from first_app.models import AccessRecord, Topic, Webpage # Register your models here. admin.site.register(AccessRecord) admin.site.register(Topic) admin.site.register(Webpage)
创建超级用户:(“root@38c80f8e29c3:/code” 说明当前主机是容器)
1 2 3 4 5 6
root@38c80f8e29c3:/code# python manage.py createsuperuser Username (leave blank to use 'root'): ck Email address: willxxx@gmail.com Password: Password (again): Superuser created successfully.
import os # Configure settings for project # Need to run this before calling models from application! os.environ.setdefault('DJANGO_SETTINGS_MODULE','first_project.settings')
import django # Import settings django.setup()
import random from first_app.models import Topic,Webpage,AccessRecord from faker import Faker
defadd_topic(): t = Topic.objects.get_or_create(top_name=random.choice(topics))[0] t.save() return t
defpopulate(N=5): ''' Create N Entries of Dates Accessed '''
for entry in range(N):
# Get Topic for Entry top = add_topic()
# Create Fake Data for entry fake_url = fakegen.url() fake_date = fakegen.date() fake_name = fakegen.company()
# Create new Webpage Entry webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]
# Create Fake Access Record for that page # Could add more of these if you wanted... accRec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]
if __name__ == '__main__': print("Populating the databases...Please Wait") populate(20) print('Populating Complete')
执行populate_first_app.py
1 2 3
root@c271100f853b:/code# python populate_first_app.py Populating the databases...Please Wait Populating Complete