Solution 1 :

Because the object uses str representation in the City model. So whenever you access that object using print or str, which your template does, the name will be returned:

def __str__(self):
    return force_text(self.name)

You should use {{ city.pk }} instead of {{ city }} or whatever unique field is available in your template.

See Python str() and repr() functions for further information about string representation.

Problem :

I have an autocomplete dropdown list that uses the list of cities from my database. I am using django-cities. I want to be able to display it as
(city.name , city.country.name) in my HTML. But it says that city is a string. Could someone explain to me why in my destination_form.html that {{ city }} is a string and not a city object

views.py

def add_destination(request,trip_id):
    city_list = City.objects.all()
    dict = {
        'city_list':city_list,
        'trip_id':trip_id,
    }
    if request.method=="POST":
        print("Resquest: ")
        print(request.POST)
        city = request.POST['city']
        print(city.country)

    return render(request,'trips/destination_form.html',dict)

destination_form.html

{% extends "trips/trip_base.html" %}
{% load bootstrap3 %}
{% block trip_content %}
<h4>Add Destination!</h4>
<form method="POST" id='destinanationForm' action="{% url 'trips:add_destination' trip_id=trip_id%}">
  {% csrf_token %}
  <label for="city">City: </label>
  <input id="city" list="city_list" placeholder="Search City..." class="form-control" style="width:300px" name="city">
  <datalist id="city_list">
    {% for city in city_list %}
    <option value="{{ city }}" >{{city.name}} , {{ city.country.name }}</option>
    {% endfor %}
  </datalist>
  <label for="start_date">Start Date: </label>
  <input id="start_date" type="date" name="start_date" value="{{ start_date }}">
  <label for="end_date">End Date: </label>
  <input id="end_date" type="date" name="end_date" value="{{ end_date }}">
<input type="submit" class="btn btn-primary btn-large" value="Create">
</form>
{% endblock %}

urls.py

path('single_trip/<str:trip_id>/add_destination',views.add_destination,name='add_destination'),

By