开发者

Django: ForeignKey with choices=Customer.objects.filter(account=self.account)

开发者 https://www.devze.com 2022-12-14 18:58 出处:网络
class Ticket(models.Model): \"\"\" An order 开发者_如何学Cplaced by a customer. \"\"\" account = models.ForeignKey(Account)
class Ticket(models.Model):
    """
    An order 开发者_如何学Cplaced 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?


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消