我使用Django 1.8.3和Rest Framework以及json-api(https://github.com/django-json-api/django-rest-framework-json-api)。我有这个OneToOne关系:
class CalendarBlock(models.Model):
vehiclecheck = models.OneToOneField('vehiclecheck.VehicleCheck',
null=True, blank=True,
related_name='calendar_block'
)
[...]
class VehicleCheck(models.Model):
[...]
现在的问题是,这种关系可能是空的"。当从CalendarBlock转到Vehiclecheck时,哪个有效,但不是反向关系:
In [1]: from vehiclecheck.models import VehicleCheck
In [2]: from dispo_calendar.models import CalendarBlock
In [3]: CalendarBlock.objects.first().vehiclecheck
In [4]: # no problem here
In [5]: VehicleCheck.objects.first().calendar_block
Out[5]: <CalendarBlock: CalendarBlock object>
In [6]: VehicleCheck.objects.get(pk=398).calendar_block
---------------------------------------------------------------------------
RelatedObjectDoesNotExist Traceback (most recent call last)
<ipython-input-6-65d3178686f5> in <module>()
----> 1 VehicleCheck.objects.get(pk=398).calendar_block
/home/sh/gitty/work/tcs_cardispo2_backend/.venv/lib/python3.5/site-packages/django/db/models/fields/related.py in __get__(self, instance, instance_type)
468 "%s has no %s." % (
469 instance.__class__.__name__,
--> 470 self.related.get_accessor_name()
471 )
472 )
RelatedObjectDoesNotExist: VehicleCheck has no calendar_block.
编辑:我的主要问题是,由于我使用的是rest_framework,我无法使用异常处理等,因为我没有明确地调用访问数据
答案 0 :(得分:6)
Django在OneToOne和ForeignKey相关字段方面存在一些问题。具体假设:
class A(Model):
# assume these models exist
b = ForeignKey(B, null=True)
c = ForeignKey(C)
d = OneToOneField(D, null=True, related_name="a")
e = OneToOneField(E, related_name="a")
我给你的例子与框架中的设计决策有关,而不是错误。此外,重要的事实并不是它们是新实例,而是它们具有空引用(或者没有O2O分别引用它们)。总结:
clean(self)
中验证其字段的新创建的模型尤其如此,并且用户可能没有填充此类字段,您可以检索它。这同样适用于OneToOne。答案 1 :(得分:1)
根据您的定义,每个CalendarBlock
实例都会vehiclecheck
为null或不为null,同样不适用于VehicleCheck
个实例,您将需要检查首先,这样你就可以避免RelatedObjectDoesNotExist
。你可以这样做:
vehicle_check = VehicleCheck.objects.get(pk=398)
if hasattr(vehicle_check, 'calendar_block'):
calendar_block = vehicle_check.calendar_block