How to filter choices in Django2’s autocomplete_fields?

If you are using autocomplete_fields for a ManyToManyField on ‘self’, this example will exclude the current object. Get the current object’s id by overriding get_form: field_for_autocomplete = None def get_form(self, request, obj=None, **kwargs): if obj: self.field_for_autocomplete = obj.pk return super(MyAdmin, self).get_form(request, obj, **kwargs) Next, override get_search_results. Modify the queryset only for your model’s autocomplete URI: … Read more

How to return 404 page intentionally in django

When you set debug to False, you don’t have a custom handler, and the status code of the response is 404, the 404.html (if present) in your base template directory is used. To return a response with a 404 status, you can simply return an instance of django.http.HttpResponseNotFound. The reason you got a 500 is … Read more

Start celery worker throws “no attribute ‘worker_state_db'”

The bug appears if an exception is raised while parsing settings. Such as when we set Django’s SECRET_KEY (or any other setting) via an environment variable: SECRET_KEY = os.environ[‘SECRET_KEY’] To solve the problem you can switch back to: SECRET_KEY = “asdfasdfasdf” or use: SECRET_KEY = os.environ.get(‘SECRET_KEY’, ”) You can also find which setting caused the … Read more

Django. TemplateDoesNotExist in case of a custom widget

If you want to use a custom widget template stored somewhere under your “TEMPLATES” directory of your project then follow these steps: a) Use the TEMPLATES settings that you have provided in your question b) Set the FORM_RENDERER as following in the settings.py FORM_RENDERER = ‘django.forms.renderers.TemplatesSetting’ c) Add the app “django.forms” to the ‘INSTALLED_APPS‘ list … Read more

GeoDjango on Windows: “Could not find the GDAL library” / “OSError: [WinError 126] The specified module could not be found”

I have found the following to work for windows: Run python to check if your python is 32 or 64 bit. Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64: Note: Select Express Web-GIS Install and click next. In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also … Read more

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