0

i have a generate(variable) function in another file, i want to call it in my models.py file. here is my code.

class Season(models.Model):
    Year = models.CharField(max_length=6, default=getyear())
    start_date = models.DateField()
    end_date = models.DateField()
    league = models.ManyToManyField(League)
    fixgen = models.BooleanField(default=False)
    in_progress = models.BooleanField(default=True)

    def __unicode__(self):
        return self.Year

    def createfixtures(self):
        generate(self)

but when i run the file, it gives me the following error.

Validating models...
Unhandled exception in thread started by <function inner_run at 0xa05eae4>
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/django/core/management/commands/runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/lib/pymodules/python2.7/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/lib/pymodules/python2.7/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/lib/pymodules/python2.7/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/usr/lib/pymodules/python2.7/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name)
  File "/usr/lib/pymodules/python2.7/django/db/models/loading.py", line 78, in load_app
   models = import_module('.models', app_name)
  File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
  File "/home/yousuf/PycharmProjects/CricketManager/../CricketManager/Cricket/models.py", line 4, in <module>
    from Cricket.fixtures import generate
  File "/home/yousuf/PycharmProjects/CricketManager/Cricket/fixtures.py", line 3, in <module>
    from Cricket.models import League, Season, Team, Fixture
  File "/home/yousuf/PycharmProjects/CricketManager/Cricket/models.py", line 4, in <module>
    from Cricket.fixtures import generate
ImportError: cannot import name generate

can i import a function that works fine if i call it from django shell. now i want to have it as part of django admin menu.

if somebody can look at it, and see what the problem is, or what is the work around for it.

//yousuf

2 Answers 2

2

Without seeing your project structure it's hard to say exactly why the import fails. However, most normally, the "fixtures" directory of an app just contains JSON/XML/whatever files and is therefore not treated as a module (by having an __init__.py file inside).

If there is not an __init__.py file in that directory, the directory is not a python module, and, therefore can't be imported. The simplest solution would be to obviously just add this file (it can be empty; it just needs to exist). However, because of what I stated above, putting python code in a directory name "fixtures" is a bad idea. It breaks convention for what that directory name is most commonly used for and thus becomes a point of confusion to other developers.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you have a circular import between Cricket.models and Cricket.fixtures. You will need to move common definitions out into a third module to break the cycle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.