Django: Foreign Key with choice = Customer.objects.filter (…

综合编程 2017-12-30

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?

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.

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

9- vue django restful framework 打造生鲜超市 -用户登录和手机注册(... Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 手机注册和用户登录(上) drf的token登录和原理 用户的下单...
Django开发简单的blog 本项目基于学习阶段,只是涉及简单的crud与模板变量等操作. 1.创建blog app项目: python manage.py startapp blog,并在项目主目录的setting...
python3开发进阶-Django框架起飞前的准备 阅读目录 一、安装(安装最新LTS版): Django官网下载页面 根据官方的图版本,我们下载1.11版本的,最好用! 有两种下载方式一种直接cmd里: p...
A Better Javascript Workflow with Django Historically, Javascript lived in one really large file. If you wanted to break up the file, you had...
Python高级进阶(二)Python框架之Django写图书管理系统(LMS)... 正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使...