From fa30f7fd660c06dadda7aed93976de53908e9d5a Mon Sep 17 00:00:00 2001 From: codehero7386 <56253286+codehero7386@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:43:02 +0530 Subject: [PATCH] Update models.py --- django_tests/models.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/django_tests/models.py b/django_tests/models.py index 0fad0c0e..ee303f0b 100644 --- a/django_tests/models.py +++ b/django_tests/models.py @@ -1,27 +1,51 @@ from six import python_2_unicode_compatible - from cloudinary.models import CloudinaryField from django.db import models - @python_2_unicode_compatible class Poll(models.Model): - id = models.AutoField(primary_key=True) + """ + Represents a poll with a question and an optional image. + """ question = models.CharField(max_length=200) + image = CloudinaryField('image', null=True, width_field='image_width', height_field='image_height') image_width = models.PositiveIntegerField(null=True) image_height = models.PositiveIntegerField(null=True) - image = CloudinaryField('image', null=True, width_field='image_width', height_field='image_height') + created_at = models.DateTimeField(auto_now_add=True) + modified_at = models.DateTimeField(auto_now=True) def __str__(self): return self.question + def save(self, *args, **kwargs): + """ + Custom save method to update modified_at timestamp. + """ + self.modified_at = timezone.now() + super(Poll, self).save(*args, **kwargs) @python_2_unicode_compatible class Choice(models.Model): - id = models.AutoField(primary_key=True) - poll = models.ForeignKey(Poll, on_delete=models.CASCADE) - choice = models.CharField(max_length=200) - votes = models.IntegerField() + """ + Represents a choice in a poll and the number of votes it has received. + """ + poll = models.ForeignKey(Poll, on_delete=models.CASCADE, related_name='choices') + choice_text = models.CharField(max_length=200) + votes = models.PositiveIntegerField(default=0) def __str__(self): - return self.choice.encode() + return self.choice_text + + def vote(self): + """ + Increment the vote count for this choice. + """ + self.votes += 1 + self.save() + + def save(self, *args, **kwargs): + """ + Custom save method to update poll's modified_at timestamp. + """ + self.poll.save() # Update the parent poll's modified_at timestamp + super(Choice, self).save(*args, **kwargs)