我真的很茫然,因为这根本没有意义。我调用保存/创建一个对象,它不会出现在管理站点中。我什至用 SQLite Viewer 程序检查了 SQLite 数据库,它再次显示该项目尚未保存。
这是保存Data
对象的代码:
data = Data(represents=field, for_entry=entry, value="This can be anything")
# field is a DataField db object and entry is a Entry db object (see model below and paragraph)
print("B4", data) #<---- Shows that Data has not been assigned an ID/pk
data.save()
print("8ER: ", data) #<--- Shows that Data has been assigned an ID/pk
正如您从我的评论中看到的,我知道 Data 对象在save
调用后被分配了一个 ID,我认为这意味着它有效。任何地方都不会抛出错误。field
, 并且entry
都是。每个似乎都很好,因为它们具有正确的 ID,用 检索[table name].objects.get(id=...)
,我可以保存/编辑它们并且它们的保存更改。
甚至奇怪的是,在这个函数工作之前调用的函数中的这个确切代码。
这是我的model.py(我去掉了一些函数让它更短):
class Entry(models.Model):
parent = models.ForeignKey('Entry', blank = True, null = True, default=None) # The entry this subs. Is left blank for top level entries.
id_number = models.CharField(max_length=20)
visible = models.BooleanField()
data_field = models.ForeignKey('DataField', default=1) # The field that this entire entry falls under. REDUNDANT BUT NECISSARY
def __str__(self):
return str(self.id)+ "-" + str(self.id_number)
class DataField(models.Model):
parent = models.ForeignKey('DataField', related_name='parent field', null=True, blank=True, default=1)
order = models.IntegerField()
multiple_entries = models.BooleanField(default=True)
DATA_TYPES = (('t', 'Text'), ('d', 'Date'), ('l', 'List'), ('o', 'Option'), ('b', 'Boolean'), ('f', 'Foreign Key'), ('r', 'Root'), ('bb', 'Branch'), ('i', 'Image'), ('ff', 'File'), ('h', 'Holder'), ('bt', 'Big Text')) # A number means it is a foreign key. Should go off title.
foreign_source = models.ForeignKey('DataField', null=True, blank=True) # Points to DATA FIELD WHO'S MATCHING DATA WILL MAKE UP THE OPTIONS
data_type = models.CharField(max_length=2, choices=DATA_TYPES, default='t', null=True, blank=True)
title = models.CharField(max_length=100, null=True, blank=True)
visibility = models.BooleanField(default=False)
def __str__(self):
return str(self.id) + "-" + str(self.title)
def __eq__(self, other):
if not isinstance(other, DataField):
return False
if self.data_type == 'h':
return self.title == other.title
return self.id == other.id
class Data(models.Model):
represents = models.ForeignKey('DataField')
for_entry = models.ForeignKey('Entry', null=True)
value = models.CharField(max_length=1000000)
def __str__(self):
return self.represents.title + "-" + str(self.for_entry.id) + "-" + str(self.value) + "-" + str(self.id)
我可能遗漏了一些明显的东西,或者您需要的信息比我提供的更多信息来修复它。如果没有足够的信息,请发表评论并索取更多信息,或者仅列出可能发生的问题。