[英]Django - build a query with these models
The goal is: 目标是:
Admin logs in to see a list of all registered members in his association. 管理员登录以查看其协会中所有已注册成员的列表。 Admin does not want to see members who are in other associations but only in his association.
管理员不希望看到其他协会中的成员,而只希望看到其协会中的成员。
Example: 例:
Coach (Admin) want to see all of his players in Real Madrid (association) and are not interested in seeing other soccer clubs (associations) players in his team. 教练(Admin)希望看到他在皇家马德里(协会)的所有球员,并且不希望看到他团队中的其他足球俱乐部(协会)的球员。
I have tried for a long time and ran out of ideas, so hopefully you guys have some great ideas i can try =D 我已经尝试了很长时间并且用尽了所有想法,所以希望你们有一些很棒的想法我可以尝试= D
Still a newbie so appreciate your help! 还是新手,感谢您的帮助!
admin\\models.py 管理员\\ models.py
class Administrator(AbstractUser):
...
asoc_name = models.CharField(max_length=100)
class Meta:
db_table = 'Administrator'
member\\models.py 成员\\ models.py
from pl.admin.models import Administrator
class Member(models.Model):
member_no = models.AutoField(primary_key=True)
asoc_name = models.CharField(max_length=100)
...
class Meta:
db_table = 'Member'
class Association(models.Model):
asocnumber = models.AutoField(primary_key=True)
asoc_name = models.CharField(max_length=100)
user = models.ForeignKey(Administrator)
member_no = models.ForeignKey(Member)
class Meta:
db_table = 'Association'
views.py views.py
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if not form.is_valid():
return render(request, 'admin/signup.html',
{'form': form})
else:
...
asoc_name = form.cleaned_data.get('asoc_name')
...
Administrator.objects.create_user(...
asoc_name=asoc_name,
...)
user = authenticate(...
asoc_name=asoc_name,
...)
return redirect('/')
else:
return render(request, 'admin/signup.html',
{'form': SignUpForm()})
Let me know if you need more info! 让我知道您是否需要更多信息!
Ok, so you're linking these models up using CharFields, which can technically work, but you probably want to use ForeignKeys
. 好的,所以您要使用CharFields将这些模型链接起来,从技术上讲可以正常工作,但是您可能要使用
ForeignKeys
。
Potentially, this means having code that looks like: 潜在地,这意味着拥有如下代码:
class Administrator(AbstractUser):
...
asoc = models.ForeignKey(Association)
class Meta:
db_table = 'Administrator'
class Member(models.Model):
member_no = models.AutoField(primary_key=True)
asoc = models.ForeignKey(Association)
...
class Meta:
db_table = 'Member'
class Association(models.Model):
asocnumber = models.AutoField(primary_key=True)
asoc_name = models.CharField(max_length=100)
member_no = models.ForeignKey(Member)
class Meta:
db_table = 'Association'
You can then make the kinds of queries you want. 然后,您可以进行所需的查询。 Example: given an administrator user, get all the members of his assosiation.
示例:给定管理员用户,获取其关联的所有成员。
Member.objects.filter(asoc=admin.asoc)
Edit 编辑
Ok, when you create the Administrator user you need to pass in an association. 好的,当您创建管理员用户时,您需要传递一个关联。 One way to do this is to have selecting the association be a part of the sign up process.
一种方法是让选择关联成为注册过程的一部分。 So for example you could add a
ModelChoiceField
to your sign up form, or something like that, or have the admin be placed in a default association. 因此,例如,您可以将
ModelChoiceField
添加到注册表单或类似的表单中,或者将管理员置于默认关联中。 But the idea is that you have an association object ready when you then create the user, and you pass it in to Administrator.create_user. 但是,这样做的想法是,当您创建用户时,已经准备好关联对象,并将其传递给Administrator.create_user。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.