Django:模型中用户的两个外键,获取不是加载页面的用户的外键
Django: two foreign keys to users in model, get the one that's not the user who loaded the page
我有两个模型:
class UserProfile(models.Model):
account = models.ForeignKey(MyUser, related_name='profiles') # MyUser is my custom user model
display_name = models.CharField(max_length=30)
...
def get_all_contracts(self):
#returns all contracts associated with the profile
class Contract(models.Model):
...
employee = models.ForeignKey(UserProfile, related_name='employee_contracts')
employer = models.ForeignKey(UserProfile, related_name='employer_contracts')
...
我想在 Contract 模型中编写一个方法,该方法 return 是 UserProfile
不是访问视图的实例,因此如果是员工访问仪表板,则此方法会 return 雇主,反之亦然,这样我就可以像这样使用它:
{% for contract in user_profile.get_all_contracts %}
<h2>The other user is {{ contract.other_user.display_name }}</h2>
{% endfor %}
在此示例中,other_user
将是方法。
有办法吗?我无法弄清楚如何从方法中检测哪个配置文件是加载页面的配置文件。
感谢任何帮助:)
您可以将此作为模板标记来执行。类似于:
from django import template
register = template.Library()
@register.simple_tag
def get_other_user_display_name(user, contract):
try:
return [obj for obj in [contract.employee, contract.employer] \
if obj != user][0].display_name
# if you don't like list comprehensions, you could do:
# set([contract.employee, contract.employer]) - set([user])
# but I find this syntax to be less than obvious
except IndexError:
return ''
然后在您的模板循环中:
{% for contract in user_profile.get_all_contracts %}
<h2>The other user is {% get_other_user_display_name request.user contract %}</h2>
{% endfor %}
如果您 100% 确信 contract.employee
和 contract.employer
关系不会为空,则可以消除该 IndexError
异常处理程序。
或者,如果您需要访问 other_user
:
的其他属性,您可以将其作为赋值标记来执行
@register.assignment_tag(takes_context=True)
def get_other_user(context, contact):
# you can get request.user from the context
user = context['request'].user
return [obj for obj in [contract.employee, contract.employer] if obj != user][0]
然后在你的循环中你可以访问你想要的任何属性。
{% for contract in user_profile.get_all_contracts %}
{% get_other_user contract as other_user %}
<h2>The other user is {{ other_user.display_name }}</h2>
<p>Their email is: {{ other_user.email }}</p>
{% endfor %}
我有两个模型:
class UserProfile(models.Model):
account = models.ForeignKey(MyUser, related_name='profiles') # MyUser is my custom user model
display_name = models.CharField(max_length=30)
...
def get_all_contracts(self):
#returns all contracts associated with the profile
class Contract(models.Model):
...
employee = models.ForeignKey(UserProfile, related_name='employee_contracts')
employer = models.ForeignKey(UserProfile, related_name='employer_contracts')
...
我想在 Contract 模型中编写一个方法,该方法 return 是 UserProfile
不是访问视图的实例,因此如果是员工访问仪表板,则此方法会 return 雇主,反之亦然,这样我就可以像这样使用它:
{% for contract in user_profile.get_all_contracts %}
<h2>The other user is {{ contract.other_user.display_name }}</h2>
{% endfor %}
在此示例中,other_user
将是方法。
有办法吗?我无法弄清楚如何从方法中检测哪个配置文件是加载页面的配置文件。
感谢任何帮助:)
您可以将此作为模板标记来执行。类似于:
from django import template
register = template.Library()
@register.simple_tag
def get_other_user_display_name(user, contract):
try:
return [obj for obj in [contract.employee, contract.employer] \
if obj != user][0].display_name
# if you don't like list comprehensions, you could do:
# set([contract.employee, contract.employer]) - set([user])
# but I find this syntax to be less than obvious
except IndexError:
return ''
然后在您的模板循环中:
{% for contract in user_profile.get_all_contracts %}
<h2>The other user is {% get_other_user_display_name request.user contract %}</h2>
{% endfor %}
如果您 100% 确信 contract.employee
和 contract.employer
关系不会为空,则可以消除该 IndexError
异常处理程序。
或者,如果您需要访问 other_user
:
@register.assignment_tag(takes_context=True)
def get_other_user(context, contact):
# you can get request.user from the context
user = context['request'].user
return [obj for obj in [contract.employee, contract.employer] if obj != user][0]
然后在你的循环中你可以访问你想要的任何属性。
{% for contract in user_profile.get_all_contracts %}
{% get_other_user contract as other_user %}
<h2>The other user is {{ other_user.display_name }}</h2>
<p>Their email is: {{ other_user.email }}</p>
{% endfor %}