Search code examples
pythonmysqldjangoweb-applicationsdirectory-structure

cannot import name "Contact"


I am trying to develop my first Django application, this is my directory tree:

demo-project
|____(html)
|____(myproject)
    |___db_dump.sql
    |___manage.py
    |___(myapp)
    |   |___(templates)
    |   |___ __init__.py
    |   |___ forms.py
    |   |___ models.py
    |   |___ tests.py
    |   |___ views.py
    |
    |___(myproject)
        |___ __init__.py
        |___ settings.py
        |___ urls.py
        |___ wsgi.py

And here are my models.py

from django.db import models
from django.forms import ModelForm
from myapp.forms import mwa_event_form


class mwa_user(models.Model):
    unique_id = models.IntegerField()
    email = models.CharField(max_length=75)
    firstName = models.CharField(max_length=40)
    lastName = models.CharField(max_length=40)
    gender = models.CharField(max_length=30)
    birthday = models.CharField(max_length=20)
    fbIdUser = models.CharField(max_length=20)
    os = models.CharField(max_length=30)


class Contact(models.Model):
    subject = models.CharField(max_length=100)
    message = models.CharField(max_length=100)
    sender = models.EmailField(max_length=100)


class mwa_event_model(ModelForm):
    class Meta:
        model = mwa_event_form
        fields = ['bfirstName', 'blastName']

and forms.py

from myapp.models import Contact
from django import forms


class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField(max_length=100)
    sender = forms.EmailField(max_length=100)


class mwa_user_form(forms.Form):
    unique_id = forms.IntegerField()
    email = forms.CharField(max_length=75)
    firstName = forms.CharField(max_length=40)
    lastName = forms.CharField(max_length=40)
    gender = forms.CharField(max_length=30)
    birthday = forms.CharField(max_length=20)
    fbIdUser = forms.CharField(max_length=20)
    os = forms.CharField(max_length=30)


class mwa_event_form(forms.Form):
    class Meta:
        model = Contact
        fields = ['bfirstName', 'blastName']

when I try to issue the command python manage.py runserver I get warned by the following:

File "/var/lib/stickshift/51e9c62e500446b4bf0000e0/approot/data/563815/myproject/myapp/models.py", line 3, in <module>  
from myapp.forms import mwa_event_form 

File "/var/lib/stickshift/51e9c62e500446b4bf0000e0/approot/data/563815/myproject/myapp/forms.py", line 1, in <module>
from myapp.models import Contact

ImportError: cannot import name Contact

What am I missing? I read in similar posts that this could be related to a circular dependency, but I did not really get what this means looking at the Django documentation. Is this related to the directory folders structure?


Solution

  • You should put your mwa_event_model from models.py into your forms.py file as it is a form. This will fix the circular import noticed in @Wooble's answer.

    As a side-note, perhaps you should read the python style guide "PEP8" for the naming convention of classes.

    Edit: I accidentally wrote mwa_event_form in my initial answer.