views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
from .models import *
def getfrom(request):
book = Book.objects.get(id=1)
m = book.id
return HttpResponse(m)
urls.py
from django.contrib import admin
from django.urls import path
from item0 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('getfrom/',views.getfrom)
]
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
m = models.IntegerField(null= True)
name = models.CharField(null= True,max_length=20)
终端测试操作
python manage.py shell 进入sql命令
from item0.models import *
book = Book(id=1,m=2,name="m")
book.save()
Book.objects.all()
Out[6]: <QuerySet [<Book: Book object (1)>]>
book = Book.objects.get(id=1)
book.id
book.name
https://blog.csdn.net/yanpenggong/article/details/82316514