1

I am trying to get a len of a textfield in Django so I can manipulate it in my template since the name of the school is too long, it is overlapping in the PDF display. I found this old post here in SO How to get the length of a TextField in Django?

I am trying to get the length of a school field which is related to another model.

x = Recipient.objects.get(id='1234551') len(x.department.school) 

This gives me an error: object of type 'Recipient' has no len(). School is also a field and it seems to work on the old post. I just don't understand why it doesn't work in my end.

2
  • Sorry, forgot to mention that Department is a foreignkey of Recipient and School is Foreignkey of Department. Commented Apr 17, 2018 at 0:23
  • You will need to include your models so we can help better Commented Apr 17, 2018 at 0:27

1 Answer 1

1

What kind of field is x.department.school ? Is that the CharField or the ForeignKey?

Or maybe it has to be something like len(x.department.school.name) ?

You will have to post your models for us to give a complete answer.

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

2 Comments

school is a foreignkey. You're correct Sir, I should've put it the x.department.school.name. May I know if len() works in FK too?
To my knowledge, len() does NOT work on ForeignKey fields, since those are usually integers in the database

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.