How to Solve ImportError in Django?

Viewed 80

I'm new to django and I'm facing some difficulties in creating a user from the AbstractUser model. Now I'm starting wondering if my user model is not being build in the right way. This is my Owner model

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import CASCADE, BooleanField
from user_app.forms import LoginForm
        
        
class user_type(AbstractUser):
    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: BooleanField = 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()

and there another import error occurred in forms.py. this is my form.py

from django import forms
from user_app.models import user_type
    
    
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 = user_type
        fields = ('username', 'email', 'password1', 'password2', 'is_user','is_insurance', 'is_police', 'is_rto')

and when I'm running this project using 'python manage.py runserver' then it shows

 File "D:\PROJECT\Digital-Vehicle-Project\digi_vehicle\user_app\models.py", line 7, in <module>
    from user_app.forms import LoginForm
  File "D:\PROJECT\Digital-Vehicle-Project\digi_vehicle\user_app\forms.py", line 2, in <module>
    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)
1 Answers

In the Owner model, in this line:

login_id = models.ForeignKey(LoginForm, on_delete=models.DO_NOTHING)

The foreign key needs to be attributed to a model only. But in your code, there is a ForeignKey associated with a form. So, code a model here. After this no need to import this:

from user_app.forms import LoginForm

Hence, it will avoid the circular import error you are facing.

Related