views:

92

answers:

2
class Ticket(models.Model):
    """
    An order placed by a customer.
    """
    account = models.ForeignKey(Account)
    client = models.ForeignKey(Client, choices=Client.objects.filter(account=self.account))

Obviously this wouldn't work because there is no instance available for 'self',but you can see what I'm trying to do here. I have a system where you can have an account. Your account can have clients, and clients can create tickets. I obviously don't want account holder A to be able to login to the system and create a ticket and assign it to account holder B's client. How would I limit the choices like this, or am I going about this all wrong in the first place?

+1  A: 

ForeignKey.limit_choices_to allows that.

Another option is to override the save() method of your model to check it (if the relation is too complex to specify the way limit_choices_to allows. I don't think you'll need it in your case).

Ofri Raviv
+1  A: 

Do your account holders use django admin interface or your ordinary custom views to assign tickets? If it is the second case, you should use customize ModelForm.

Antony Hatchkins