30👍
It’s “correct”, PEP8 just flags lines over 79 characters long. But if you’re concerned about that, you could write it like this:
field = TreeForeignKey('self',
null=True,
blank=True,
related_name='abcdefgh')
Or this:
field = TreeForeignKey(
'self',
null=True,
blank=True,
related_name='abcdefgh',
)
Or, really, any other style that would break the single line into multiple shorter lines.
18👍
I just found this neat program called autopep8! https://github.com/hhatto/autopep8
pip install autopep8
autopep8 -i models.py
You can also do (recursively):
autopep8 -ri package/
Auto PEP8 only makes safe changes to the files, only changing layout, not code logic.
- [Django]-Django – Highlight Navigation based on current page?
- [Django]-Dirty fields in django
- [Django]-How to test "render to template" functions in django? (TDD)
9👍
If you have some ridiculous long string that isn’t very convenient to break into pieces (thinking about things like Sentry DSNs, the occasional module in MIDDLEWARE or INSTALLED_APPS), you can just put # noqa
at the end of the line and the linters will ignore the line. Use sparingly thou and definitely not for the case you asked for.
- [Django]-Django – Where are the params stored on a PUT/DELETE request?
- [Django]-Django order_by() filter with distinct()
- [Django]-How can I set the field unique in django?
5👍
This is very subjective. I’d write, if I were following E501 strictly:
field = TreeForeignKey('self',
null=True,
blank=True,
related_name='abcdefgh')
I usually consider 100 too long, not 80.
- [Django]-Django createview how to get the object that is created
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Django: request.GET and KeyError
0👍
I usually split that to line up the parameters one level of indentation deeper than the original line, like:
field = TreeForeignKey('self', null=True,
blank=True, related_name='abcdefgh')
Especially if TreeForeignKey
is something like TreeForeignKeyWithReferencesToSomethingElse
, in which case all the parameters would start out to the far right of the window if you lined them all up with the opening parenthesis. If any of the parameters had a long name like defaultvalueforcertaincircumstances
, you might not be able to fit the whole thing in under 80 columns:
field = TreeForeignKeyWithReferencesToSomethingElse('self',
defaultvalueforcertaincircumstances='foo')
I also prefer to put multiple function parameters on the same line (except when it just doesn’t look right; I’m not a purist!) so that vertical space isn’t overly expanded, causing me to spend more time scrolling around in my editor than otherwise necessary.
- [Django]-How to save a model without sending a signal?
- [Django]-Django error: relation "users_user" does not exist
- [Django]-How to use if/else condition on Django Templates?
0👍
autopep8
can solve spacing issues in files and entire project as well, here is a link to the documentation: https://github.com/hhatto/autopep8
pip install --upgrade autopep8
autopep8 --in-place --aggressive --aggressive <path_with_python_filename>
ex:autopep8 --in-place --aggressive --aggressive C://a/test.py
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Use Django ORM as standalone
- [Django]-How to See if a String Contains Another String in Django Template