On Tue, 2007-12-25 at 14:34 -0800, [EMAIL PROTECTED] wrote: > Hello, > > Having the following (Postgre)SQL statement: > > CREATE TABLE Games ( > minPlayer integer NOT NULL DEFAULT 1, > maxPlayer integer NOT NULL DEFAULT 1, > CHECK (min_player <= max_player) > ) > > the consequent Model for Django would be: > > class Game(model.Models): > min_player = models.IntegerField(default=1), > max_player = models.IntegerField(default=1) > > But how can I define the "CHECK (min_player <= max_player)" > constraint? I tried overriding the save method to: > > def save(self): > if self.max_player < self.min_player: > return False > super(Game, self).save() > > and although no game will be created (when specifying max_player < > min_player), the Admin interface will output a successful creation to > the user, when it is not.
This requires model validation to be finished, which is currently work in progress. Validation and saving are two different steps and the save() method won't raise any validation errors, but it might raise database integrity errors (such as when the check constraint fails). We will shortly have code that implements validate() on models (don't get thrown by the fact there's already a validate() method on the Model class; it's incomplete and should not be used) and possibly/probably even a shortcut to allow "validate and save" in one step, but, in any case, the admin interface will make sure models are valid before trying to save them. At the moment, there's no really good workaround for this with the current admin and state of model validation. Regards, Malcolm -- Borrow from a pessimist - they don't expect it back. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

