我有如下因素模型:如何使用Django ORM进行特殊的many2many字段验证?
class Step(models.Model):
order = models.IntegerField()
latitude = models.FloatField()
longitude = models.FloatField()
date = DateField(blank=True, null=True)
class Journey(models.Model):
boat = models.ForeignKey(Boat)
route = models.ManyToManyField(Step)
departure = models.ForeignKey(Step, related_name="departure_of", null=True)
arrival = models.ForeignKey(Step, related_name="arrival_of", null=True)
我想执行以下检查:
# If a there is less than one step, raises ValidationError.
routes = tuple(self.route.order_by("date"))
if len(routes) <= 1:
raise ValidationError("There must be at least two setps in the route")
# save the first and the last step as departure and arrival
self.departure = routes[0]
self.arrival = routes[-1]
# departure and arrival must at least have a date
if not (self.departure.date or self.arrival.date):
raise ValidationError("There must be an departure and an arrival date. "
"Please set the date field for the first and last Step of the Journey")
# departure must occurs before arrival
if not (self.departure.date > self.arrival.date):
raise ValidationError("Departure must take place the same day or any date before arrival. "
"Please set accordingly the date field for the first and last Step of the Journey")
我试图通过重载save()
做到这一点。不幸的是,在save()
中为空。更何况,Journey.id
还不存在。我没有尝试django.db.models.signals.post_save
,但假设它会失败,因为Journey.route
也是空的(无论如何,这是否填充?)。我在django.db.models.signals.m2m_changed
中看到了一个解决方案,但是有很多步骤(数千),并且我希望避免为它们中的每一个执行操作。
良好的捕获,但不幸的是我不相信生产代码的不稳定版本。我的脚本已经足够了:-) – 2010-03-21 20:22:10
作为一个附注,目前主干与1.2版本的代码完全相同,并且在实际发布之前需要修复一些错误。 我们一直在生产应用程序中追踪树干一年,并且没有任何问题。 – knutin 2010-03-22 10:21:51