我为模型编写了一个自定义保存方法,以防止保存无效数据,但是当我想通过admin更新对象(只是更改某些属性)时,我得到了断言错误
我的模特:
class Segment(CoreModel):
departure = models.ForeignKey(Place, on_delete=models.CASCADE, limit_choices_to=limit_choices_segment,
related_name='departures')
destination = models.ForeignKey(Place, on_delete=models.CASCADE, limit_choices_to=limit_choices_segment,
related_name='arrivals')
distance = models.FloatField(help_text='Distance between places in "km"!', null=True, blank=True,
validators=[property_positive_value])
duration = models.FloatField(help_text='Transfer duration (hours)', null=True, blank=True,
validators=[property_positive_value])
cost = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True,
help_text='Price for a transfer! Currency: "UAH"!',
validators=[property_positive_value])
route = models.ForeignKey(Route, on_delete=models.CASCADE, related_name='segments')
def __str__(self):
return '{}-{}'.format(self.departure, self.destination)
def save(self, *args, **kwargs):
assert self.departure.role not in (Place.DISTRICT, Place.REGION), (
"Departure couldn't be Region or District")
assert self.destination.role not in (Place.DISTRICT, Place.REGION), (
"Destination couldn't be Region orDistrict")
assert self.destination != self.departure, "Departure couldn't be equal to Destination"
assert self.route.segment_validate(departure=self.departure, destination=self.destination), (
'Impossible to add the segment, please check the route!')
if self.distance is not None:
assert self.distance > 0, "Distance couldn't be less or equal to '0'!"
if self.duration is not None:
assert self.duration > 0, "Duration couldn't be less or equal to '0'!"
if self.cost is not None:
assert self.cost > 0, "Cost couldn't be less or equal to '0'!"
super(Segment, self).save(*args, **kwargs)
验证方法:
def segment_validate(self, departure, destination):
segments = self.segments.all()
if segments:
for segmnet in segments:
same_departure = segmnet.departure == departure
same_destination = segmnet.destination == destination
if ((same_departure and same_destination) or
same_departure or same_destination):
return False
if segments.latest('created').destination != departure:
return False
return True
错误在这里:
assert self.route.segment_validate(departure=self.departure, destination=self.destination), (
'Impossible to add the segment, please check the route!')
但我没有更改departure
和destination
您能帮我避免这个错误吗?
答案 0 :(得分:0)
出现此问题的原因是,如果您已经保存了一个段,现在您要问数据库中是否存在一个段,它们的段 same department
或{{ 1}}。如果您没有更新destination
中的department
,那么当然有:该特定段。
如果存在,我们可以将destination
从其中排除,
segment
因此,我们通过以下方式对此进行检查:
from django.db.models import Q
def segment_validate(self, departure_pk, destination_pk, segment_pk):
segments = self.segments.exclude(pk=segment_pk)
if not segments.filter(
Q(departure_id=departure_pk) |
Q(destination_id=destination_pk)
).exists():
return False
if segment_pk is None:
return segments.latest('created').destination_id != departure_pk
return True