考虑以下模型:
from django.db import models
class Album(models.Model):
id = models.IntegerField(primary_key=True,null=False)
artist = models.CharField(max_length=200)
album_title = models.CharField(max_length = 250)
genre = models.CharField(max_length=100)
album_logo = models.CharField(max_length = 200)
def __str__(self):
return "id = " + str(self.id) + " artist = " + self.artist + " album = " + self.album_title + " genre = " + self.genre
class Song(models.Model):
id = models.IntegerField(primary_key=True)
album = models.ForeignKey(Album,on_delete=models.CASCADE)
file_type = models.CharField(max_length = 200)
song_title = models.CharField(max_length = 200)
def __str__(self):
return "id = " + str(self.id) + "album = " + self.album.album_title + "song = " + self.song_title
当我使用位置参数在Album或Song中插入行而不给出时,django正在为该特定行提供 NULL 。为什么这样?
答案 0 :(得分:3)
首先,没有必要明确声明一个名为'id'的字段,因为Django默认情况下已经为每个模型创建了一个id字段并自动递增它。
在任何情况下,如果您想故意声明主键,建议您按以下方式进行:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("G557") <> Range("G555") Then
MsgBox "It has to be equal"
End If
End Sub
这是一个自动递增的主键。
参考官方Django文档: https://docs.djangoproject.com/en/1.11/topics/db/models/#automatic-primary-key-fields