0
votes

Hi i follow djangogirls tutorial for learning django

in the tutorial they use

author = models.ForeignKey('auth.User', on_delete=models.CASCADE)

But they import nothing for auth.User Why? i see in many tutorials this code:

from django.contrib.auth.models import User

whats diffrent beetween calling without import and calling with import

Thanks.

1
There is no semantical difference. But importing things can result in trouble: if you have circular imports, then Python can not run the program. It is thus often used to fix circular imports. Furthermore both are usually not a good idea, since one can use another User model.Willem Van Onsem
@WillemVanOnsem So the good way is calling without import?user8354377
Yes, in model definitions only. It won't work in other code.Daniel Roseman
Not per se, both have advantages. The main advantage of using imports is that IDEs understand this better, and thus in case one renames a model, then it is renamed in the reference as well.Willem Van Onsem
@DanielRoseman Thanks for your helpuser8354377

1 Answers

0
votes

Its used to avoid running into a circular import situation,

But even better is using get_user_model() see here in the docs