[英]Access RelatedManager object in django
我有以下模型:
class Travel(models.Model):
purpose = models.CharField(max_length=250, null=True)
amount = models.IntegerField()
class TravelSection(models.Model):
travel = models.ForeignKey(Travel, related_name='sections', on_delete=models.CASCADE)
isRoundTrip = models.BooleanField(default=True)
distance = models.FloatField()
transportation = models.CharField(max_length=250)
我在不使用數據庫的情況下創建對象:
mytravel = Travel(
purpose='teaching',
amount=1
)
mysection = TravelSection(
travel=mytravel,
isRoundTrip=True,
distance=500,
transportation='train'
)
當我嘗試訪問mytravel.sections
時,我無法訪問字段distance
和其他字段。 我怎樣才能做到這一點而不必保存在數據庫中? 我不想在數據庫中創建對象,因為我正在嘗試進行一些預處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.