Currently trying to create a Models.py file for my Django project to be able to store questions in a database.
But every time i reference the model in my forms.py for the Meta Class I receive an import error.
forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Q1
Question1_CHOICES = (
('1', 'I Like Smoking'),
('2', 'I Dislike Smoking'),
('3', 'I Do not Smoke'),
('4', 'I Do not mind Smokers '),
)
class QuestionForm(forms.Form):
Q1 = forms.MultipleChoiceField(
required=False,
widget=forms.RadioSelect,
choices=Question1_CHOICES
)
class Meta:
model = Q1
fields = ['question']
widgets = {
'question': forms.RadioSelect()
}
Models.py
from django.db import models
from .forms import Question1_CHOICES
class Q1(models.Model):
question = models.CharField(max_length=50, choices=Question1_CHOICES)
My Error is as follows
File "forms.py", line 3, in from .models import Q1
Any help would be really appreciated as I'm stumped what it could be
You have a circular import
forms.py is importing models.py
models.py is importing forms.py
I would recommend moving Question1_CHOICES
into models.py. Follow the docs as an example https://docs.djangoproject.com/en/1.10/ref/models/fields/#choices
Try this
Models.py
from django.db import models
Question1_CHOICES = ( ('1', 'I Like Smoking'), ('2', 'I Dislike Smoking'), ('3', 'I Do not Smoke'), ('4', 'I Do not mind Smokers '), )
class Q1(models.Model):
question = models.CharField(max_length=50, choices=Question1_CHOICES)
Forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Q1, Question1_CHOICES
class QuestionForm(forms.Form):
q1 = forms.MultipleChoiceField( required=False, widget=forms.RadioSelect, choices=Question1_CHOICES )
class Meta:
model = Q1 fields = ['question']
widgets = { 'question': forms.RadioSelect() }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.