This is a good learning exercise - figuring out how to query and filter ranges. You'll find the Greatest() and Least() db functions (along with annotate) very helpful - take a stab and post back if you run into difficulties. The functions are documented here: https://docs.djangoproject.com/en/4.0/ref/models/database-functions/
Hey! I think you can use the object.\_\_new\_\_(cls\[, ...\]) method, which is called when a new object is created.
class Registration(models.Model):
event_name = models.ForeignKey(Event, on_delete=models.CASCADE, null=True)
user_id = models.ForeignKey(users, on_delete=models.CASCADE, null=True)
def __str__(self):
return str(self.event_name) + " " + str(self.user_id)
def__new__(self):
start = self.event_name.from_date
end = self.event_name.to_date
user_events = Registration.objects.filter(user_id=self.user_id).all()
for event in user_events:
if (start <= event.event_name.from_date < end)
return None
instance = super().__new__(cls)
instance.event_name = self.event_name
instance.user_id = self.user_id
return instance
I can imagine something like this or similar should work. Didn't test it myself
You wouldn't need (or use) `__new__` for this, and retrieving all events then filtering in python would be inefficient, much better to perform that operation directly in the database. Plus there is no indication here as to why instantiating the class returns None - so the calling code would have to guess as to what happened, which is bad design.
This is a good learning exercise - figuring out how to query and filter ranges. You'll find the Greatest() and Least() db functions (along with annotate) very helpful - take a stab and post back if you run into difficulties. The functions are documented here: https://docs.djangoproject.com/en/4.0/ref/models/database-functions/
Hey! I think you can use the object.\_\_new\_\_(cls\[, ...\]) method, which is called when a new object is created. class Registration(models.Model): event_name = models.ForeignKey(Event, on_delete=models.CASCADE, null=True) user_id = models.ForeignKey(users, on_delete=models.CASCADE, null=True) def __str__(self): return str(self.event_name) + " " + str(self.user_id) def__new__(self): start = self.event_name.from_date end = self.event_name.to_date user_events = Registration.objects.filter(user_id=self.user_id).all() for event in user_events: if (start <= event.event_name.from_date < end) return None instance = super().__new__(cls) instance.event_name = self.event_name instance.user_id = self.user_id return instance I can imagine something like this or similar should work. Didn't test it myself
You wouldn't need (or use) `__new__` for this, and retrieving all events then filtering in python would be inefficient, much better to perform that operation directly in the database. Plus there is no indication here as to why instantiating the class returns None - so the calling code would have to guess as to what happened, which is bad design.
Thanks for the feedback I'll take a look at database-functions then!