Reputation: 527
Model:
class Subject(models.Model):
name = models.CharField(max_length=50)
places = models.IntegerField()
class Temp(models.Model):
subject_r = models.ForeignKey(Subject)
Now in my views I want to do simple thing: If Subject id exists in Temp table, field places (which is integer) need to be decremented by 1, else, it should stay as it is, and then I want to display the correct places variable in my template. Note that i don't want to save new value in a database, i just want it to be displayed correctly in a template.
Upvotes: 0
Views: 382
Reputation: 118508
temp = Temp.objects.values_list('subject_r_id', flat=True)
subjects = Subject.objects.all()
for subject in subjects:
if subject.id in temp:
subject.places = subject.places - 1
Since it's for display only, I would actually set an arbitrary new attribute so that it's clear you're not trying to modify the database.
temp = Temp.objects.values_list('subject_r_id', flat=True)
subjects = Subject.objects.all()
for subject in subjects:
if subject.id in temp:
subject.places_display = subject.places - 1
else:
subject.places_display = subject.places
{{ subject.places_display }}
Upvotes: 1