码迷,mamicode.com
首页 > 其他好文 > 详细

manytomany django

时间:2018-11-01 00:51:18      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:any   djang   title   cas   rem   end   textfield   print   post   

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)

 

manytomany django

标签:any   djang   title   cas   rem   end   textfield   print   post   

原文地址:https://www.cnblogs.com/pythonClub/p/9886428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!