GithubHelp home page GithubHelp logo

Comments (6)

cwittlinger avatar cwittlinger commented on June 22, 2024 1

see my edited answer

from mkautodoc.

apelliciari avatar apelliciari commented on June 22, 2024 1

hi @cw-intellineers @tomchristie

where would you call django.setup()?

because I have DJANGO_SETTINGS_MODULE filled but if:

  • I don't put django.setup(), I have the error "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet."
  • i put django.setup() [I've not found any other "entrypoint" who can be customized for mkdocs] I get some weird error on circular imports on django-extensions field
    ImportError: cannot import name 'CreationDateTimeField' from partially initialized module 'django_extensions.db.fields' (most likely due to a circular import)

Can you help me? I'm lost.

Thanks

from mkautodoc.

tomchristie avatar tomchristie commented on June 22, 2024

I guess you’d need to set the DJANGO_SETTINGS_MODULE environment variable, before running mkdocs?

from mkautodoc.

cwittlinger avatar cwittlinger commented on June 22, 2024

Okay, apparently I was stupid and while debugging changed my .env file...stupid me...

Thanks for the help.

from mkautodoc.

tomchristie avatar tomchristie commented on June 22, 2024

🤷‍♂️ Sorry I don’t know - not something I’m really able to help with.

from mkautodoc.

jleclanche avatar jleclanche commented on June 22, 2024

So … I came across this, and ended up with a workaround I'm so ashamed of I'm starting to be proud of it.

I created a file in docs/django_settings.py, which contains the settings I want to initialize with, as well as a call to django.setup() after said settings, and a dummy variable I called setup:

import os
import sys

docs_dir, _ = os.path.split(__file__)
sys.path.append(os.path.dirname(docs_dir))

SECRET_KEY = "."
INSTALLED_APPS = ["…"]

setup = None
import django  # noqa
from django.apps import apps  # noqa
from django.conf import settings  # noqa

if not apps.ready and not settings.configured:
    django.setup()

I'm setting DJANGO_SETTINGS_MODULE to docs.settings, which in theory should be enough, but it actually isn't: Depending on the piece of code that is being autodoc'd, the import cycle can sometimes erroneously detect a circular import and raise an ImportError due to partially initialized module. One such example would be:

  1. Document ::: library.module.SomeClass
  2. From library.module, import a django setting
  3. django.setup() is called, which goes through importing every INSTALLED_APP's models.py
  4. From library.models, import library.module
  5. We're now in a circular import (even though we really aren't)

So to fix that, django.setup() would have to be called as the first thing autodoc does, so that we don't cycle back to the code that called the setting; usually that is done in the entrypoint. But autodoc doesn't have an entrypoint… soooo, I made one by adding the following code to my index.md:

<style type="text/css">
/* Hide the hack signature from the index. */
.autodoc { display: none; }
</style>

::: docs.django_settings.setup

Now as one of the first things autodoc does (as part of the first document it processes), mkautodoc will import the docs.django_settings module, which will trigger django.setup() and avoid breakage later on.

from mkautodoc.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.