'Models and forms in Django
Is forms and models run parallelly in Django? I've created a forms.py in my app and in that forms.py I've created a loginform instead of forms.py I've created AbstractUser and registration table in models.py, then import this loginform to models.py(from user_app.forms import LoginView). Then in forms.py import the models.py AbstractUser class. (I've named this class as user_type .. from user_app.models import user_type). I got this error
File "D:\PROJECT\Digital-Vehicle-Project\digi_vehicle\user_app\models.py", line 7, in from user_app.forms import LoginForm
File "D:\PROJECT\Digital-Vehicle-Project\digi_vehicle\user_app\forms.py", line 2, in
from user_app.models import user_type
ImportError: cannot import name 'user_type' from 'user_app.models' (D:\PROJECT\Digital-Vehicle-Project\digi_vehicle\user_app\models.py)
forms.py
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(
widget=forms.TextInput(
attrs={
"class": "form-control"
}
)
)
password = forms.CharField(
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
)
)
class Meta:
model = UserType
fields = ('username', 'email', 'password1', 'password2', 'is_user', 'is_insurance', 'is_police', 'is_rto')
models.py
from django.contrib.auth.base_user import AbstractBaseUser
from django.db import models
# Create your models here.
from django.db.models import CASCADE, BooleanField
from user_app.forms import LoginForm
class user_type(AbstractBaseUser):
is_user = models.BooleanField('Is user', default=False)
is_police = models.BooleanField('Is police', default=False)
is_insurance = models.BooleanField('Is insurance', default=False)
is_rto = models.BooleanField('Is rto', default=False)
class Owner(models.Model):
user_id = models.AutoField(primary_key=True)
login_id = models.ForeignKey(LoginForm, on_delete=models.DO_NOTHING)
user_name = models.CharField(max_length=255)
aadhar_number = models.CharField(max_length=16)
photo = models.FileField()
mobile = models.IntegerField()
licence = models.CharField(max_length=10)
address = models.TextField()
state = models.CharField(max_length=10)
district = models.CharField(max_length=10)
city = models.CharField(max_length=10)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
Solution 1:[1]
In forms.py you need to add the import of the UserType class like this
from user_app.models import UserType
Not like this
from user_app.models import user_type
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
