Snippet List
If the primary key on a table is an integer, it can be desirable after a lot of adding and removing either during testing (as was my case) or otherwise, to tidy up the key space a little and see the primary keys run up as unbroken sequences from 1.
An excellent snippet here:
https://djangosnippets.org/snippets/2915/
showed us how change the primary key value from in a given table and in all tables that relate to it (have a foreign key pointing into it).
We can exploit that to achieve this outcome with integrity. For any given table we just fetch the primary keys into a sorted list and then walk the list assigning key 1, 2, 3, 4 etc where needed (if it doesn't already have that key).Because our list is sorted we're always moving tuples down the ladder of available keys to next empty slot basically until we've compacted the whole list.
And voila. Mission accomplished.
Works in with database introspection, in part because the integrity of relations is a little obscured by the ORM which hides intermediary tables in ManyToMany relationships and such. At the database API level these concerns all disappear.
Built and tested with Django 1.11 but the form only goes to 1.10 here. Not even sure it works on 1.10. Certainly the snippet I based it on didn't work in 1.11 and need some updating.
A simple Python function that converts a Python datetime formatting string to its nearest PHP equivalent.
Python and PHP use different format string conventions when specifying datetime formats:
* Python: [https://docs.python.org/2/library/time.html#time.strftime](https://docs.python.org/2/library/time.html#time.strftime)
* PHP: [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php)
Working with Django the Date, Time and DateTime widgets all use Python format strings as stored in:
* django.conf.global_settings.DATE_INPUT_FORMATS
* django.conf.global_settings.TIME_INPUT_FORMATS
* django.conf.global_settings.DATETIME_INPUT_FORMATS
but if you want to use a datetime widget like the datetimepicker here:
[http://xdsoft.net/jqplugins/datetimepicker/](http://xdsoft.net/jqplugins/datetimepicker/)
then you'll find it uses PHP format specifiers.
If you want Django and the datetimepicker to populate a field in the same way, you need to ensure they use the same format.
So I add to the Django from context the default format as follows:
`context["default_datetime_input_format"] = datetime_format_python_to_PHP(DATETIME_INPUT_FORMATS[0])`
and in the template Javascript on my form for the datetimepicker i give it:
`"format": {{default_datetime_input_format}}`
and the datetimepicker now populates the the datetime field in the same format as Django.
- datetime
- date
- format
- time
bernd-wechner has posted 3 snippets.