models
from django.db import models from django.contrib.auth.models import User class GroupSheet(models.Model): group=models.CharField(max_length=32,null=False,unique=True) gid=models.CharField(max_length=128,null=False,unique=True) #1$1, 1$1$2, 1$10$21 class QuestionSheet(models.Model): title=models.CharField(max_length=128,null=False) narritive=models.TextField() group=models.ForeignKey(GroupSheet,on_delete=models.CASCADE) user=models.ForeignKey(User,on_delete=models.CASCADE) postdate=models.DateTimeField(auto_now=True) class AnswerSheet(models.Model): question=models.ForeignKey(QuestionSheet,on_delete=models.CASCADE) answer=models.TextField() postdate=models.DateTimeField(auto_now=True) user=models.ForeignKey(User,on_delete=models.CASCADE) plus=models.BooleanField() plus_answer=models.ManyToManyField('AnswerSheet',related_name='test')
execute via view:
from django.shortcuts import render from django.views import View from yufei.models import * class IndexView(View): def get(self,request): 增加 obj=AnswerSheet.objects.get(id=9) follow=AnswerSheet.objects.get(id=6) obj.plus_answer.add(follow) #要用字段名 删除 obj=AnswerSheet.objects.get(id=9) follow=AnswerSheet.objects.get(id=6) obj.plus_answer.remove(follow) #要用字段名 #查询 obj = AnswerSheet.objects.get(id=9) b=obj.plus_answer.all() print('正查',b) obja = AnswerSheet.objects.get(id=6) c = obja.test.all() print('反查', c) return render(request,'index.html')