Python Django 4.2.5教程:c.delete()级联删除数据

>>> c=q.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()
(1, {
    
    'polls.Choice': 1})
>>> Choice.objects.all()

在这里插入图片描述

vim django-20231002/mysite/polls/models.py

from django.db import models

# Create your models here.
class Question(models.Model):
    question_text=models.CharField(max_length=200)
    pub_date=models.DateTimeField(name='published_time')
    def __str__(self) -> str:
        return self.question_text
    
    class Meta:
        verbose_name="议题"
        verbose_name_plural="议题"

class Choice(models.Model):
    question=models.ForeignKey(Question,on_delete=models.CASCADE)    
    choice_text=models.CharField(max_length=200,default='')
    votes=models.IntegerField(default=1)
    # def __str__(self) -> str:
    #     return self.id
    class Meta:
        verbose_name="选票"
        verbose_name_plural="选票"

猜你喜欢

转载自blog.csdn.net/a772304419/article/details/133527325