tags:

views:

34

answers:

0

Given the following models.py:

class Computer(models.Model):  
    mac_address=models.CharField()  
    serial_number=model.CharField()  
    hostname=models.CharField() 

class CheckOut(models.Model):  
    computer=models.ForeignKey('Computer')

How can I limit the choices displayed in the Admin's dropdown based on whether that computer has already been checked out? The models.py above allows someone to checkout the same computer more than once. My initial thought was to put a boolean field in Computer that would switch from False to True when the CheckOut model got saved, and then add limit_choices_to. Something like this:

class Computer(models.Model):  
    mac_address=models.CharField()  
    serial_number=model.CharField()  
    hostname=models.CharField()  
    is_checked_out=models.BooleanField(default=False)

class CheckOut(models.Model):  
    computer=models.ForeignKey('Computer',limit_choices_to={'is_checked_out':False}) 

def save(self, *args, **kwargs):
    #update is_checked_out to True
    return super(CheckOut, self).save(force_insert, force_update, *args, **kwargs)

Is this possible with a save override? If so can someone give me some hints on the syntax? If this isn't possible (or non-sensical), what would be a better method/model? I also considered using signals, but that seemed way more complex than necessary. Thanks...