django database

时间的设定
from django.db import models import django.utils.timezone as timezone class Doc(models.Model): add_date = models.DateTimeField('保存日期',default = timezone.now) mod_date = models.DateTimeField('最后修改日期', auto_now = True

 

When the html page reads the DateTimeField field from the database, the displayed time format is inconsistent with the format stored in the database. For example, the content of the database field is 2016-06-03 13:00:00, but the page displays Apr. 03, 2016 , 1 pm

In order to display the same page and database, you need to format the time on the page, you need to add a filter like <td>{{ infor.updatetime|date:"Ymd H:i:s"  }}</td>. Refresh the page to display normally.



from django.db import models
from datetime import datetime
import django.utils.timezone as timezone
# Create your models here.


# 1: 简单用户表 tb_user:
# id , username,userpaw
class user(models.Model):
user_name = models.CharField(max_length=20)
user_paw = models.CharField(max_length=20)
def __str__(self):
return (self.user_name, self.user_paw)


# 2: 用户详细信息表 tb_userinfo
# id,userid , email , homepage , phone , address ...

class user_info(models.Model):
user_id = models.ForeignKey("user", on_delete=models.CASCADE)
email = models.CharField(max_length=30)
phone = models.CharField(max_length=16)
sex = models.CharField(max_length=5)
def __str__(self):
return (self.user_id, self.email, self.phone, self.sex)



# 3: Forum topic table tb_bbs
# id , userid , title , ip , count ,create_time, up
class bbs(models.Model):
user_id = models.ForeignKey("user",on_delete=models.CASCADE)
title = models.CharField(max_length=50)
create_time = models.DateTimeField()
count = models. IntegerField(default=timezone.now)
up = models.IntegerField()
def __str__(self):
return (self.user_id, self.title, self.create_time, self.count, self.up)


# 4: Forum content label tb_bbs_content (This table can be stored in separate tables according to bbsid)
# id,bbsid , content;
class bbs_content(models.Model):
bbs_id = models.ForeignKey("bbs", on_delete=models.CASCADE)
content = models.CharField(max_length=200)
def __str__(self):
return (self.bbs_id, self.content)


# 5: 论坛回复表 tb_bbs_reply (此表可按照bb_sid进行分表存储)
# id , bbs_id , user_id , content , time , ip
class bbs_reply(models.Model):
bbs_id = models.ForeignKey("bbs", on_delete=models.CASCADE)
user_id = models.ForeignKey("User", on_delete=models.CASCADE)
content = models.CharField(max_length=150)
time = models.DateTimeField(default=timezone.now)
def __str__(self):
return (self.bbs_id, self.user_id, self.content, self.time)



table operations

Adding table records

Method 1:
Book()
b=Book(name="python basic",price=99,author="yuan",pub_date="2017-12-12")
b.save()
Method 2:
Book .objects.create()
Book.objects.create(name="oldboy linux",price=78,author="oldboy",pub_date="2016-12-12")


Modification of table records
Method 1:

b=Book.objects.get(author="oldboy")
b.price=120
b.save()

Method 2:
#update is QuerySet
Book.objects.filter(author="yuan") Deletion of .update (price=999)

table records:
Book.objects.filter(author="oldboy").delete()

Query of table records (emphasis):

book_list = Book.objects.filter(id=2)
book_list= Book.objects.exclude(author="yuan").values("name","price")

book_list=Book.objects.all()
book_list = Book.objects.all()[::2]
book_list = Book. objects.all()[::-1] #first

, last, get is an instance object, not a collection object of QuerySet
book_list = Book.objects.first()
book_list = Book.objects.last()
book_list = Book.objects.get(id=2)#No error is reported when only one record can be taken out


ret1=Book.objects.filter(author="oldboy").values("name")
ret2=Book.objects.filter(author="yuan").values_list("name","price")

book_list= Book.objects.all().values("name").distinct()
book_count= Book.objects.all().values("name").distinct().count()


Fuzzy query double underscore__

book_list=Book.objects.filter(name__icontains="P").values_list("name","price")
book_list=Book.objects.filter(id__gt=5).values_list("name","price")


Multiple tables Operation (one-to-many): #Add
record
#publish_id=2
Book.objects.create(name="linux operation and maintenance",price=77,pub_date="2017-12-12",publish_id=2)

#publish=object
Book.objects.create(name="GO",price=23,pub_date="2017-05-12",publish=publish_obj) #Query

records (through objects)

Forward query:
book_obj=Book.objects .get(name="python") pub_obj=book_obj.publish----" Reverse query
of the publisher object
pub_obj.name corresponding to the book object: pub_obj = Publish.objects.filter(name="People's Publishing House")[ 0] pub_obj.book_set.all().values("name","price") #Query records (filter values ​​double underscore__) #Books and prices published by People's Publishing House ret=Book.objects.filter(publish__name ="People's Publishing House").values("name","price") #python The name of the book publisher ret2=Publish.objects.filter(book__name="python").values("name") #python The name of the publisher of this book ret3=Book.objects.filter(name="python").values("publish__name") #The name of the book published by the publishing house in Beijing
















ret4=Book.objects.filter(publish__city="Beijing").values("name") #The

name of the publisher that published the book in the first half of 2017
ret5=Book.objects.filter(pub_date__lt="2017-07-01 ",pub_date__gt="2017-01-01").values("publish__name")


multi-table operation (many-to-many):

create a many-to-many relationship author= models.ManyToManyField("Author") (recommended)


book object it All associated authors of obj=book_obj.authors.all()
bind a many-to-many relationship obj.add(*QuerySet)
obj.remove(author_obj)


If you want to insert a value into the third table, the binding relationship: Create manually third table

# class Book_Author(models.Model):
# book=models.ForeignKey("Book")
# author=models.ForeignKey("Author")
Book_Author.objects.create(book_id=2,author_id=3)


Master: by filter values (Double down line) for many-to-many association query (form and one-to-many)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325091900&siteId=291194637