0

I am trying to return an object from a database in Django. So I have this line in my views.py:

  single_appointment = Appointment.objects.get(id=int(app_id))

app_id is passed into the view as an argument.

I seem to be able to access the database, but the only problem is that if I print the single_appointment in the console, it prints just one attribute of the appointment object.

How do I get the whole object so I can access its attributes?

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
ruslaniv
  • 458
  • 1
  • 6
  • 14
  • 1
    When you print an object, you see the string representation of it. It doesn't print you the whole object. However it's still the whole object and you can access all of its attributes. – viam0Zah Jun 22 '20 at 10:41
  • print(single_appointment.__dict__) – Hoff Jun 22 '20 at 10:44
  • Or see [How do I look inside a Python object?](https://stackoverflow.com/questions/1006169/how-do-i-look-inside-a-python-object) – viam0Zah Jun 22 '20 at 10:45
  • @viam0Zah Yes, you're absolutely right! First, I did not write the `__str__` methods for the class (actually I wrote the wrong ones, which is even worse, lol). And yes, the attributes were all there, just sitting behind the dot. Could you put it as an answer, so I can accept it? – ruslaniv Jun 22 '20 at 10:50
  • 1
    The `str(..)` of a model object by default returns something like `` (with `pk` the primary key), you can override `__str__` to change this and access the attributes to obtain the other data. – Willem Van Onsem Jun 22 '20 at 11:02

0 Answers0