django ForeignKey null = true

3
from django.db import models

class Story(models.Model):
    id = models.IntegerField(primary_key=True)
    news_type = models.CharField(max_length=255,null=True)
    category_id = models.CharField(max_length=255,null=True)
    title = models.CharField(max_length=255,null=True)
    created = models.DateTimeField(null=True)
    author = models.CharField(max_length=255, null=True)
    author_title = models.CharField(max_length=255, null=True)
    image_caption = models.TextField(null=True)
    image_credit = models.CharField(max_length=255,null=True)
    image_full_url = models.CharField(max_length=255,null=True)
    body = models.TextField(null=True)
    summary = models.TextField(null=True)
    video_id = models.CharField(max_length=255,null=True)
    external_url = models.CharField(max_length=255,null=True)
    order = models.IntegerField(null=True)

class StoryFactBox(models.Model):
    story = models.ForeignKey('Story', null = True)
    body = models.TextField()

class StoryKeyword(models.Model):
    story = models.ForeignKey('Story', null = True)
    keyword = models.CharField(max_length=255)
< p > models.ForeignKey('Story', null = True) 会引起哪些模式更改?

我正在阅读文档:

我想使用remove()和clear()方法,这是文档的一部分。

为了避免数据库不一致,仅当null=True时,此方法仅存在于ForeignKey对象上。如果无法将相关字段设置为None(NULL),则无法从关系中删除对象而不将其添加到另一个对象中。在上面的示例中,从b.entry_set()中删除e等效于执行e.blog = None,并且因为blog ForeignKey没有null=True,所以这是无效的。

1个回答

4

这不会改变模式(只是澄清它只创建表,不修改),但在同步数据库时,会创建没有NOT NULL的SQL语句。

您可以通过python manage.py sqlall my_app检查表定义的SQL输出,并自行查看!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接