0

I want to get the username using the field user (foreignkey)

I don't know how get the username in the view

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 %} 
1
  • 1
    I'm not familiar with django filtersets. but usually you can access related field with the double underscore syntax: user__username. Commented Mar 1, 2019 at 16:28

1 Answer 1

1

You can use '__' : Publication.objects.filter(user__username="John Doe")

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.