zoukankan      html  css  js  c++  java
  • Django MySQL数据库操作

    上一篇文章写了一些基本的Django操作,下面重点介绍数据库的内容。

    对象之间的关系:

    1. 一对一
    2. 一对多
    3. 多对多

    1.一对多

    先演示一对多的关系,多个blog对应一个名字, 修改blog/models.py

    class Entry(models.Model):
            name = models.CharField(max_length=30)
    
            def __unicode__(self):
                    return self.name
    
    class Blog(models.Model):
            name = models.CharField(max_length=30)
            entry = models.ForeignKey(Entry)
    
            def __unicode__(self):
                    return self.name
    #在djang1.9以后,数据库同步执行指令如下:
    #同步数据库接口(注意需要切换至python project工作空间所在路径)
    python manage.py makemigrations 
    #同步数据
    python manage.py migrate

    填入数据时,必须首先填入外键,即这里的name。

    学会基本的创建和查询操作

    from blog.models import Entry, Blog
    entry1 = Entry.objects.create(name='alen')
    entry2 = Entry.objects.create(name='lee')
    entry3 = Entry.objects.create(name='anna')
    
    blog1 = Blog.objects.create(name='alen_blog1', entry=entry1)
    blog1.entry
    blog1.id
    entry1.blog_set.all()

    2.多对多

    作者和书之间的关系,多对多。

    class Author(models.Model):
            name = models.CharField(max_length=30)
    
            def __unicode__(self):
                    return self.name
    
    class Book(models.Model):
            name = models.CharField(max_length=30)
            authors = models.ManyToManyField(Author)
    
            def __unicode__(self):
                    return self.name
    from blog.models import Author, Book
    Author.objects.create(name='Alen')
    Author.objects.create(name='eric')
    Author.objects.create(name='lee')
    Author.objects.create(name='zhang')
    authors = Author.objects.all()
    authors
    
    b1 = Book()
    b1.name = 'python book1'
    b1.save()
    b1.authors.add()
    alen = Author.objects.get(name__exact='Alen')
    b1.authors.add(alen)
    b1.authors.add(authors[1])
    b1.authors.all()
    b1.authors.add(authors[2])
    b1.authors.add(authors[3])
    b1.authors.all()
    b1.authors.remove(alen)
    b1.authors.all()
    b1.authors.filter(name__exact='lee')
    alen.book_set.all()
    alen.book_set.add(b1)
    alen.book_set.create(name='python book2')
    books = Book.objects.all()
    alen.book_set.remove(book[0])
    alen.book_set.remove(books[0])
    alen.book_set.all()

    3.数据库数据网页显示

    插一句,今天居然遇到了Ubuntu可以ssh连接,却无法上网的尴尬局面,估计是配置Apache的时候把系统搞乱了。搞了好久才发现是DNS解析问题,导致无法使用域名,但可以使用IP。

    编辑/etc/resolv.conf文件

    添加一行

    nameserver  8.8.8.8

    无意中把Django给卸载了,又重装了,以为网站访问不了,其实不用怕,很简单,创建prj和app,然后全部copy过去,网站就会恢复啦。

    4.管理界面

    sex_choices = {
            ('f', 'female'),
            ('m', 'male'),
    }
    class User(models.Model):
            name = models.CharField(max_length=30)
            sex = models.CharField(max_length=1, choices=sex_choices)

    为blog应用添加用户。

    django使用bootstrap快速美化 admin后台(新版不可用)

    使用suit-v2美化Django Admin(兼容Django新版本!)

    admin后天样式丢失,怎么解决?

  • 相关阅读:
    如何用RadioButton做一个底部的切换栏
    自定义有监听器的ScrollView
    ViewPager的使用小技巧
    什么时候用Application的Context,什么时候用Activity的Context
    让改变输入法回车键的图标
    墙内下载DropBox离线安装包的方法
    巧用用layer-list做一个卡片背景
    LeakCanary中英文文档+使用例子
    让你的APK瘦成一道闪电
    用一张图片实现按钮按下和普通效果的样式
  • 原文地址:https://www.cnblogs.com/leezx/p/7051615.html
Copyright © 2011-2022 走看看