What is related_name used for?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don’t specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all(). If you do specify, e.g. related_name=maps on the User model, User.map_set will still work, but the User.maps. syntax is obviously a bit cleaner and less clunky; so for example, if you … Read more

Iterating over dictionary items(), values(), keys() in Python 3

I’m not sure if this is quite an answer to your questions but hopefully it explains a bit about the difference between Python 2 and 3 in this regard. In Python 2, iter(d.keys()) and d.iterkeys() are not quite equivalent, although they will behave the same. In the first, keys() will return a copy of the dictionary’s list of keys and iter will then … Read more

Python initializing a list of lists

The problem is that they’re all the same exact list in memory. When you use the [x]*n syntax, what you get is a list of n many x objects, but they’re all references to the same object. They’re not distinct instances, rather, just n references to the same instance. To make a list of 3 different lists, do this: This gives you 3 … Read more

“for loop” with two variables?

If you want the effect of a nested for loop, use: If you just want to loop simultaneously, use: Note that if x and y are not the same length, zip will truncate to the shortest list. As @abarnert pointed out, if you don’t want to truncate to the shortest list, you could use itertools.zip_longest. UPDATE Based on the request for “a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)