Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom model method in Django
    text
    copied!<p>I'm developing a web app in Django that manages chores on a reoccurring weekly basis. These are the models I've come up with so far. Chores need to be able to be assigned multiple weekdays and times. So the chore of laundry could be Sunday @ 8:00 am and Wednesday @ 5:30 pm. I first want to confirm the models below are the best way to represent this. Secondly, I'm a little confused about model relationships and custom model methods. Since these chores are on a reoccurring basis, I need to be able to check if there has been a CompletedEvent in this week. Since this is row level functionality, that would be a model method correct? Based on the models below, how would I check for this? It has me scratching my head.</p> <p>models.py:</p> <pre><code>from django.db import models from datetime import date class ChoreManager(models.Manager): def by_day(self, day_name): return self.filter(scheduledday__day_name = day_name) def today(self): todays_day_name = date.today().strftime('%A') return self.filter(scheduledday__day_name = todays_day_name) class Chore(models.Model): objects = ChoreManager() name = models.CharField(max_length=50) notes = models.TextField() class Meta: ordering = ['scheduledday__time'] class ScheduledDay(models.Model): day_name = models.CharField(max_length=8) time = models.TimeField() chore = models.ForeignKey('Chore') class CompletedEvent(models.Model): date_completed = DateTimeField(auto_now_add=True) chore = models.ForeignKey('Chore') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload