internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: django-extensions/django-extensions

Issue:
Regression with Python 2 and graph_models
After merging #877 there is regression in `graph_models` command when all of the following conditions are met:

- running on Python 2;
- there are `verbose_name` set for some `ForeignKey`/`ManyToManyFIeld` which contains non ASCII characters;
- `graph_models` is executed with `--verbose-names` argument.

I will try to prepare a PR.
Regression with Python 2 and graph_models
After merging #877 there is regression in `graph_models` command when all of the following conditions are met:

- running on Python 2;
- there are `verbose_name` set for some `ForeignKey`/`ManyToManyFIeld` which contains non ASCII characters;
- `graph_models` is executed with `--verbose-names` argument.

I will try to prepare a PR.


Discussion:

Resolving patch:
diff --git a/django_extensions/management/modelviz.py b/django_extensions/management/modelviz.py
--- a/django_extensions/management/modelviz.py
+++ b/django_extensions/management/modelviz.py
@@ -8,8 +8,6 @@
   Adapted to be used with django-extensions
 """
 
-# from __future__ import unicode_literals
-
 import datetime
 import os
 import re
@@ -151,7 +149,7 @@ def add_relation(self, field, model, extras=""):
             related_query_name = field.related_query_name()
             if self.verbose_names and related_query_name.islower():
                 related_query_name = related_query_name.replace('_', ' ').capitalize()
-            label = '{} ({})'.format(label, force_text(related_query_name))
+            label = u'{} ({})'.format(label, force_text(related_query_name))
 
         # handle self-relationships and lazy-relationships
         if isinstance(field.remote_field.model, six.string_types):


Test patch:
diff --git a/tests/management/test_modelviz.py b/tests/management/test_modelviz.py
--- a/tests/management/test_modelviz.py
+++ b/tests/management/test_modelviz.py
@@ -18,5 +18,10 @@ def test_render_unicode_field_label(self):
         data = generate_graph_data(app_labels, verbose_names=True)
         models = data['graphs'][0]['models']
         model = [x for x in models if x['name'] == 'UnicodeVerboseNameModel'][0]
-        s = model['fields'][1]['label']
-        self.assertEqual(u'Café', s)
+        fields = dict((_f['name'], _f['label']) for _f in model['fields'])
+        expected = {
+            'id': u'ID',
+            'cafe': u'Café',
+            'parent_cafe': u'Café latte',
+        }
+        self.assertEqual(expected, fields)
diff --git a/tests/testapp/models.py b/tests/testapp/models.py
--- a/tests/testapp/models.py
+++ b/tests/testapp/models.py
@@ -247,9 +247,13 @@ class Meta:
 
 class UnicodeVerboseNameModel(models.Model):
     cafe = models.IntegerField(verbose_name=u'café')
+    parent_cafe = models.ForeignKey('self', related_name='children',
+                                    on_delete=models.CASCADE,
+                                    verbose_name='café latte')
 
     class Meta:
         app_label = 'django_extensions'
+        verbose_name = u'café unicode model'
 
 
 class Permission(models.Model):
Repository: django-extensions/django-extensions

Issue:
RemovedInDjango30Warning: Remove the context parameter from JSONField.from_db_value()
This deprecation warning is shown on Django 2.1:

```
RemovedInDjango30Warning: Remove the context parameter from JSONField.from_db_value(). Support for it will be removed in Django 3.0.
```

The solution from https://github.com/python-social-auth/social-app-django/pull/173/files looks sensible?


Discussion:

Resolving patch:
diff --git a/django_extensions/db/fields/encrypted.py b/django_extensions/db/fields/encrypted.py
--- a/django_extensions/db/fields/encrypted.py
+++ b/django_extensions/db/fields/encrypted.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 import warnings
 
+import django
 import six
 from django import forms
 from django.conf import settings
@@ -82,8 +83,12 @@ def to_python(self, value):
             retval = value
         return retval
 
-    def from_db_value(self, value, expression, connection, context):
-        return self.to_python(value)
+    if django.VERSION < (2, ):
+        def from_db_value(self, value, expression, connection, context):
+            return self.to_python(value)
+    else:
+        def from_db_value(self, value, expression, connection):
+            return self.to_python(value)
 
     def get_db_prep_value(self, value, connection, prepared=False):
         if value and not value.startswith(self.prefix):
diff --git a/django_extensions/db/fields/json.py b/django_extensions/db/fields/json.py
--- a/django_extensions/db/fields/json.py
+++ b/django_extensions/db/fields/json.py
@@ -13,8 +13,9 @@ class LOL(models.Model):
 from __future__ import absolute_import
 
 import json
-import six
 
+import django
+import six
 from django.conf import settings
 from django.core.serializers.json import DjangoJSONEncoder
 from django.db import models
@@ -89,8 +90,12 @@ def get_prep_value(self, value):
             return dumps(value)
         return super(models.TextField, self).get_prep_value(value)
 
-    def from_db_value(self, value, expression, connection, context):
-        return self.to_python(value)
+    if django.VERSION < (2, ):
+        def from_db_value(self, value, expression, connection, context):
+            return self.to_python(value)
+    else:
+        def from_db_value(self, value, expression, connection):
+            return self.to_python(value)
 
     def get_db_prep_save(self, value, connection, **kwargs):
         """Convert our JSON object to a string before we save"""


Test patch:
Repository: django-extensions/django-extensions

Issue:
MySQL sqldiff duplicates AUTO_INCREMENT and UNSIGNED declarations creating false positives
e.g.

`App auth Model Permission field 'id' parameters differ: db='integer AUTO_INCREMENT AUTO_INCREMENT', model='integer AUTO_INCREMENT'`




Discussion:

Resolving patch:
diff --git a/django_extensions/management/commands/sqldiff.py b/django_extensions/management/commands/sqldiff.py
--- a/django_extensions/management/commands/sqldiff.py
+++ b/django_extensions/management/commands/sqldiff.py
@@ -354,7 +354,7 @@ def get_field_db_type(self, description, field=None, table_name=None):
         tablespace = field.db_tablespace
         if not tablespace:
             tablespace = "public"
-        if (tablespace, table_name, field.column) in self.unsigned:
+        if (tablespace, table_name, field.column) in self.unsigned and self.unsigned_suffix not in field_db_type:
             field_db_type = '%s %s' % (field_db_type, self.unsigned_suffix)
 
         return field_db_type
@@ -827,7 +827,7 @@ def get_field_db_type(self, description, field=None, table_name=None):
             # just convert it back to it's proper type, a bool is a bool and nothing else.
             if db_type == 'integer' and description[1] == FIELD_TYPE.TINY and description[2] == 1:
                 db_type = 'bool'
-            if (table_name, field.column) in self.auto_increment:
+            if (table_name, field.column) in self.auto_increment and 'AUTO_INCREMENT' not in db_type:
                 db_type += ' AUTO_INCREMENT'
         return db_type
 


Test patch:
Repository: django-extensions/django-extensions

Issue:
from django.templatetags import static is broken for (at least) django 2.2
It throws `TypeError: 'module' object is not callable` now, PR with fix incoming.


Discussion:

Resolving patch:
diff --git a/django_extensions/admin/widgets.py b/django_extensions/admin/widgets.py
--- a/django_extensions/admin/widgets.py
+++ b/django_extensions/admin/widgets.py
@@ -5,7 +5,7 @@
 from django.contrib.admin.sites import site
 from django.contrib.admin.widgets import ForeignKeyRawIdWidget
 try:
-    from django.templatetags import static
+    from django.templatetags.static import static
 except ImportError:
     # compatibility with django < 2.1
     from django.contrib.admin.templatetags.admin_static import static


Test patch:
diff --git a/tests/test_admin_widgets.py b/tests/test_admin_widgets.py
--- a/tests/test_admin_widgets.py
+++ b/tests/test_admin_widgets.py
@@ -35,3 +35,17 @@ def test_widget_works(self):
         # Just making sure rendering the widget doesn't cause any issue
         widget.render('person', person.pk)
         widget.render('person', None)
+
+        # Check media to make sure rendering media doesn't cause any issue
+        self.assertListEqual(
+            [
+                '/static/django_extensions/js/jquery.bgiframe.js',
+                '/static/django_extensions/js/jquery.ajaxQueue.js',
+                '/static/django_extensions/js/jquery.autocomplete.js',
+            ],
+            widget.media._js,
+        )
+        self.assertListEqual(
+            ['/static/django_extensions/css/jquery.autocomplete.css'],
+            list(widget.media._css['all']),
+        )
Repository: django-extensions/django-extensions

Issue:
manage.py command reset_db doesn't work with mysql.connector.django
It says that there is an unknown engine when trying to reset the database. I recommend adding 

```
'mysql.connector.django',
```
 to line 15 of the settings.py of django_extensions 


Discussion:

Resolving patch:
diff --git a/django_extensions/settings.py b/django_extensions/settings.py
--- a/django_extensions/settings.py
+++ b/django_extensions/settings.py
@@ -13,6 +13,7 @@
 DEFAULT_MYSQL_ENGINES = (
     'django.db.backends.mysql',
     'django.contrib.gis.db.backends.mysql',
+    'mysql.connector.django',
 )
 DEFAULT_POSTGRESQL_ENGINES = (
     'django.db.backends.postgresql',


Test patch:
Repository: django-extensions/django-extensions

Issue:
validate_templates can fail with an empty DIRS setting
```pytb
Traceback (most recent call last):
  File "./manage.py", line 23, in <module>
    main()
  File "./manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages
/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages
/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages
/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages
/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages/django_extensions/management/utils.py", line 62, in inner
    ret = func(self, *args, **kwargs)
  File "/Users/rik24d/.local/share/virtualenvs/imake-aTBwPyYI/lib/python3.7/site-packages/django_extensions/management/commands/validate_templates.py", line 57, in handle
    template_dirs = set(get_template_setting('DIRS'))
```


Discussion:

Resolving patch:
diff --git a/django_extensions/management/commands/validate_templates.py b/django_extensions/management/commands/validate_templates.py
--- a/django_extensions/management/commands/validate_templates.py
+++ b/django_extensions/management/commands/validate_templates.py
@@ -54,7 +54,7 @@ def handle(self, *args, **options):
             self.ignores = getattr(settings, 'VALIDATE_TEMPLATES_IGNORES')
 
         style = color_style()
-        template_dirs = set(get_template_setting('DIRS'))
+        template_dirs = set(get_template_setting('DIRS', []))
         template_dirs |= set(options['includes'])
         template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))
 


Test patch:
Repository: django-extensions/django-extensions

Issue:
Encoding passed to json.loads was deprecated to be removed in Python 3.9
`encoding` parameter passed to `json.loads` was ignored and deprecated since Python 3.1 . This will be removed in Python 3.9 .

https://github.com/django-extensions/django-extensions/blob/5208677aeb0ad9225eca1ac5d6cc3b79869f3aca/django_extensions/mongodb/fields/json.py#L41

Ref : https://bugs.python.org/issue33461


Discussion:
Same goes for:

https://github.com/django-extensions/django-extensions/blob/master/django_extensions/db/fields/json.py#L31

In April 2020 the extended support for Django 1.11 will end. I think at this time Django will not support any Python version below 3.1 and we can safely remove it.

Resolving patch:
diff --git a/django_extensions/db/fields/json.py b/django_extensions/db/fields/json.py
--- a/django_extensions/db/fields/json.py
+++ b/django_extensions/db/fields/json.py
@@ -26,11 +26,10 @@ def dumps(value):
 
 
 def loads(txt):
-    value = json.loads(
-        txt,
-        encoding=settings.DEFAULT_CHARSET
-    )
-    return value
+    if six.PY2:
+        return json.loads(txt, encoding=settings.DEFAULT_CHARSET)
+    else:
+        return json.loads(txt)
 
 
 class JSONDict(dict):
diff --git a/django_extensions/mongodb/fields/json.py b/django_extensions/mongodb/fields/json.py
--- a/django_extensions/mongodb/fields/json.py
+++ b/django_extensions/mongodb/fields/json.py
@@ -38,7 +38,10 @@ def dumps(value):
 
 
 def loads(txt):
-    value = json.loads(txt, parse_float=Decimal, encoding=settings.DEFAULT_CHARSET)
+    if six.PY2:
+        value = json.loads(txt, parse_float=Decimal, encoding=settings.DEFAULT_CHARSET)
+    else:
+        value = json.loads(txt, parse_float=Decimal)
     assert isinstance(value, dict)
     return value
 


Test patch:
Repository: django-extensions/django-extensions

Issue:
rankdir support for graph_models
For some people the way graph_models orders the nodes in `.dot` mode is not very intuitive - the most important 
entity (hospital, country, site) is placed at the bottom of the picture while I would expect it to be placed on the top.

Would it be ok to add a `--rankdir` switch for it? I would contribute the necessary patch.


Discussion:
Sure happy to merge it !

Resolving patch:
diff --git a/django_extensions/management/commands/graph_models.py b/django_extensions/management/commands/graph_models.py
--- a/django_extensions/management/commands/graph_models.py
+++ b/django_extensions/management/commands/graph_models.py
@@ -171,7 +171,14 @@ def __init__(self, *args, **kwargs):
                 'dest': 'arrow_shape',
                 'choices': ['box', 'crow', 'curve', 'icurve', 'diamond', 'dot', 'inv', 'none', 'normal', 'tee', 'vee'],
                 'help': 'Arrow shape to use for relations. Default is dot. Available shapes: box, crow, curve, icurve, diamond, dot, inv, none, normal, tee, vee.',
-            }
+            },
+            '--rankdir': {
+                'action': 'store',
+                'default': 'TB',
+                'dest': 'rankdir',
+                'choices': ['TB', 'BT', 'LR', 'RL'],
+                'help': 'Set direction of graph layout. Supported directions: "TB", "LR", "BT", "RL", corresponding to directed graphs drawn from top to bottom, from left to right, from bottom to top, and from right to left, respectively. Default is TB.'
+            },
         }
 
         defaults = getattr(settings, 'GRAPH_MODELS', None)
@@ -230,6 +237,9 @@ def handle(self, *args, **options):
         else:
             raise CommandError("Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.")
 
+        if options.get('rankdir') != 'TB' and output not in ["pydot", "pygraphviz", "dot"]:
+            raise CommandError("--rankdir is not supported for the chosen output format")
+
         # Consistency check: Abort if --pygraphviz or --pydot options are set
         # but no outputfile is specified. Before 2.1.4 this silently fell back
         # to printind .dot format to stdout.
diff --git a/django_extensions/management/modelviz.py b/django_extensions/management/modelviz.py
--- a/django_extensions/management/modelviz.py
+++ b/django_extensions/management/modelviz.py
@@ -84,6 +84,7 @@ def __init__(self, app_labels, **kwargs):
             self.app_labels = [app.label for app in apps.get_app_configs()]
         else:
             self.app_labels = app_labels
+        self.rankdir = kwargs.get("rankdir")
 
     def generate_graph_data(self):
         self.process_apps()
@@ -107,6 +108,7 @@ def get_graph_data(self, as_json=False):
             'disable_fields': self.disable_fields,
             'disable_abstract_fields': self.disable_abstract_fields,
             'use_subgraph': self.use_subgraph,
+            'rankdir': self.rankdir,
         }
 
         if as_json:


Test patch:
Repository: django-extensions/django-extensions

Issue:
RemovedInDjango41Warning: 'django_extensions' defines default_app_config
The following warning is emitted when using django-extenstions along with django 3.2
```
django.utils.deprecation.RemovedInDjango41Warning: 'django_extensions' defines default_app_config = 'django_extensions.apps.DjangoExtensionsConfig'. Django now detects this configuration automatically. You can remove default_app_config.
```

I suppose it is related to the change introduced by django 3.2: https://docs.djangoproject.com/en/3.2/releases/3.2/#what-s-new-in-django-3-2

Environment:
* python 3.8
* django 3.2



Discussion:
Note to self (or others): Probably enough to add a little version check such that we do not define `default_app_config` in 3.2

Resolving patch:
diff --git a/django_extensions/__init__.py b/django_extensions/__init__.py
--- a/django_extensions/__init__.py
+++ b/django_extensions/__init__.py
@@ -19,4 +19,11 @@ def get_version(version):
 
 __version__ = get_version(VERSION)
 
-default_app_config = 'django_extensions.apps.DjangoExtensionsConfig'
+try:
+    import django
+
+    if django.VERSION < (3, 2):
+        default_app_config = 'django_extensions.apps.DjangoExtensionsConfig'
+except ModuleNotFoundError:
+    # this part is useful for allow setup.py to be used for version checks
+    pass


Test patch:
diff --git a/tests/management/commands/test_notes.py b/tests/management/commands/test_notes.py
--- a/tests/management/commands/test_notes.py
+++ b/tests/management/commands/test_notes.py
@@ -8,7 +8,7 @@ def test_without_args(capsys, settings):
     call_command('notes')
 
     out, err = capsys.readouterr()
-    assert 'tests/testapp/__init__.py:\n  * [  4] TODO  this is a test todo\n\n' in out
+    assert 'tests/testapp/__init__.py:\n  * [  8] TODO  this is a test todo\n\n' in out
 
 
 def test_with_utf8(capsys, settings):
diff --git a/tests/test_compatibility.py b/tests/test_compatibility.py
new file mode 100644
--- /dev/null
+++ b/tests/test_compatibility.py
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+"""Test Compatility between different django versions"""
+import django
+import pytest
+
+import django_extensions
+
+
+class TestDefaultAppConfigDefinition:
+    @pytest.mark.skipif(django.VERSION < (3, 2), reason='app config is automatically defined by django')
+    def test_app_config_not_defined(self):
+        assert hasattr(django_extensions, 'default_app_config') is False
+
+    @pytest.mark.skipif(django.VERSION >= (3, 2), reason='app config is  not automatically defined by django')
+    def test_app_config_defined(self):
+        assert hasattr(django_extensions, 'default_app_config') is True
diff --git a/tests/testapp/__init__.py b/tests/testapp/__init__.py
--- a/tests/testapp/__init__.py
+++ b/tests/testapp/__init__.py
@@ -1,4 +1,8 @@
 # -*- coding: utf-8 -*-
-default_app_config = 'tests.testapp.apps.TestAppConfig'
+import django
+
+
+if django.VERSION < (3, 2):
+    default_app_config = 'tests.testapp.apps.TestAppConfig'
 
 # TODO: this is a test todo
Repository: django-extensions/django-extensions

Issue:
RandomCharField overwriting initial value
Sometimes it's necessary for the random value to be not so random. I'll expect that passing initial value to the model constructor would default to the value, but this isn't the case. E.g

```
User.objects.create(slug='this-is-not-random')
```
In the above example, I'll expect the value of the slug field (which is the RandomCharField) to be set to `this-is-not-random`, but it gets ignored and the random char generation proceeds


Discussion:

Resolving patch:
diff --git a/django_extensions/db/fields/__init__.py b/django_extensions/db/fields/__init__.py
--- a/django_extensions/db/fields/__init__.py
+++ b/django_extensions/db/fields/__init__.py
@@ -319,6 +319,9 @@ class RandomCharField(UniqueFieldMixin, CharField):
 
     include_punctuation
         If set to True, include punctuation characters (default: False)
+    
+    keep_default
+        If set to True, keeps the default initialization value (default: False)
     """
 
     def __init__(self, *args, **kwargs):
@@ -341,6 +344,7 @@ def __init__(self, *args, **kwargs):
         self.include_alpha = kwargs.pop('include_alpha', True)
         self.check_is_bool('include_alpha')
         self.include_punctuation = kwargs.pop('include_punctuation', False)
+        self.keep_default = kwargs.pop('keep_default', False)
         self.check_is_bool('include_punctuation')
         self.max_unique_query_attempts = kwargs.pop('max_unique_query_attempts', MAX_UNIQUE_QUERY_ATTEMPTS)
 
@@ -362,7 +366,7 @@ def in_unique_together(self, model_instance):
         return False
 
     def pre_save(self, model_instance, add):
-        if not add and getattr(model_instance, self.attname) != '':
+        if (not add or self.keep_default) and getattr(model_instance, self.attname) != '':
             return getattr(model_instance, self.attname)
 
         population = ''


Test patch: