我想使用字段user
(外键)获得用户名
我不知道如何在视图中获取用户名
model.py
class Publication(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
filters.py
class PublicationFilter(django_filters.FilterSet):
user = django_filters.CharFilter(lookup_expr='exact')
class Meta:
model = Publication
fields = ['user']
views.py
def publication_list(request):
f = PublicationFilter(request.GET, queryset=Publication.objects.all())
return render(request, 'info/filter.html', {'filter':f})
html
<h2>Lista de sus informes</h2>
<p class="profile-data">
<div class="col-md-4 mt-2 mb-3 ">
<div class="row p-1">
<div class="col-md-12">
<form action="" method="get" >
<b> {{ filter.form.as_p }} </b><br>
<button type="submit">Search</button>
</form>
<ul>
<b>{% for profile in filter.qs %} </b><br>
<b>{{ profile.nombre }} </b><br>
<b>{{ profile.user }} </b><br>
<a href="{% url 'profiles:detail' profile.user %}">Ver perfil</a><br>
{% endfor %}
答案 0 :(得分:1)
您可以使用'__':Publication.objects.filter(user__username="John Doe")