Reputation: 352
I have a custom save routine in one of my models that compares datetime
fields and then is supposed to modify a field on another model and save it based on the results. However, the comparison is not working as I would expect it to. Namely, my <=
comparison is returning True
regardless of the actual comparison.
models.py
class Session(models.Model):
date = models.DateField()
# etc...
class Case(models.Model):
summary = models.TextField(blank=True)
session = models.ForeignKey(Session, on_delete=models.CASCADE, related_name='cases')
case_type = models.ForeignKey(CaseType)
# etc...
class Person(models.Model):
earliest_case = models.ForeignKey('Case', null=True, blank=True, related_name='person_to_earliest_case+')
latest_case = models.ForeignKey('Case', null=True, blank=True, related_name='person_to_latest_case+')
# etc...
class Litigant(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='person_to_case')
case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name='case_to_person')
# etc...
def save(self, *args, **kwargs):
try:
person = Person.objects.get(id=self.person_id)
earliest_case = person.earliest_case
latest_case = person.latest_case
if self.case.session.date <= earliest_case.session.date:
person.earliest_case = self.case
person.save()
elif self.case.session.date >= latest_case.session.date:
person.earliest_case = self.case
person.save()
except:
pass
super(Litigant, self).save(*args, **kwargs)
As you can see, when I save a new Litigant
, what I want it to do is call up the existing Person
instance, compare that instance's entries for earliest_case
and latest_case
with the new Case
instance tied to Litigant
and then reassign the fields if necessary.
What is happen instead is that the new case
is being added to person.earliest_case
regardless of whether it is earlier or later than the existing date there, and I'm not really sure why.
For reference, the dates stored in the Session
date field are all Year-Date-Month
.
Frustratingly, if I call up two instances of case.session.date
in the shell, they compare just fine.
If it matters, the case
fk is getting added by my view like so:
views.py
def add_litigant(request, pk):
case_instance = get_object_or_404(models.Case, pk=pk)
if request.method == "POST":
form = forms.LitigantForm(request.POST)
if form.is_valid():
new_litigant = form.save(commit=False)
new_litigant.case = case_instance
new_litigant.save()
# etc...
# def edit_litigant works the same way
I would appreciate any guidance or pointers. Thanks in advance!
Upvotes: 0
Views: 98
Reputation: 1213
What is happen instead is that the new case is being added to person.earliest_case regardless of whether it is earlier or later than the existing date there, and I'm not really sure why.
That's why:
if self.case.session.date <= earliest_case.session.date:
person.earliest_case = self.case # <- assigned to earliest_case
person.save()
elif self.case.session.date >= latest_case.session.date:
person.earliest_case = self.case # <- assigned to earliest_case. COPYPASTA!
person.save()
Upvotes: 2