1

I can't understand why this isn't working because in a Python console it works fine for me...

class Activity(models.Model):
    id = models.CharField(max_length=250, primary_key=True)
    description = models.CharField(max_length=255, null=False, help_text="Brief description of the activity")
    start = models.DateTimeField(default=timezone.now, verbose_name="Planned start date/time", blank=True)
    end = models.DateTimeField(default=timezone.now, verbose_name="Planned completion date/time", blank=True)

    class Meta:
        verbose_name_plural = 'Activities'

    def save(self, *args, **kwargs):
        self.id = "%s-%s" % (self.description, str(self.start.date()))
        super(Activity, self).save(*args, **kwargs)

    def __str__(self):
        """
        String for representing the Model object (in Admin site etc.)
        """
        return f'{self.description}'

But what I get for my self.id field is (using "xx" as description):

"xx-<built-in method date of datetime.datetime object at 0x000001FCBAAA51E0>"
2
  • Does it occur for each new object? Commented Sep 2, 2018 at 17:04
  • Yes, every time Commented Sep 2, 2018 at 17:25

1 Answer 1

1

Try to use strftime instead

def save(self, *args, **kwargs):
    self.id = "%s-%s" % (self.description, self.start.strftime('%m/%d/%Y'))
    super(Activity, self).save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.