我有以下模型:
class Article(TimestampedModel):
title = models.CharField(max_length=255)
slug = models.SlugField(db_index=True, max_length=255, unique=True, null=True, blank=True)
introduction = models.TextField(null=True, blank=True)
content = HTMLField('Content')
image = models.ImageField(null=True, blank=True, upload_to='articles')
parent = models.ForeignKey(
'core.Article', on_delete=models.CASCADE, related_name='sectionparent', null=True, blank=True
)
authors = models.ManyToManyField(People, blank=True)
active = models.BooleanField(default=True)
SECTIONS = (
('about', 'About'),
('newsevents', 'News and Events'),
)
section = models.CharField(max_length=20, choices=SECTIONS, default='about')
site = models.ForeignKey(Site, on_delete=models.CASCADE)
objects = models.Manager()
on_site = CurrentSiteManager()
class Event(models.Model):
article = models.OneToOneField(
Article,
on_delete=models.CASCADE,
related_name='event',
),
EVENT_TYPE = (
('match', 'Match'),
('other', 'Other'),
)
start = models.DateTimeField(null=True, blank=True);
end = models.DateTimeField(null=True, blank=True);
type = models.CharField(max_length=20, choices=EVENT_TYPE)
location = models.CharField(max_length=255, null=True, blank=True)
url = models.CharField(max_length=255, null=True, blank=True)
但是,当我查看数据库中生成的事件表时,没有外键(我本来希望如此)。此外,我在文章中有记录:
article = Article.objects.find(1)
然后当我尝试访问该事件时:
event = article.event
我会收到此错误:
“文章”对象没有属性“事件”