简体   繁体   English

Django中的ImportError如何解决?

[英]How to Solve ImportError in Django?

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.我是 django 的新手,在从 AbstractUser model 创建用户时遇到了一些困难。现在我开始怀疑我的用户 model 是否没有以正确的方式构建。 This is my Owner model这是我的主人 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.并且在 forms.py 中发生了另一个导入错误。 this is my form.py这是我的表格.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当我使用“python manage.py runserver”运行这个项目时,它会显示

 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)

In the Owner model, in this line:在所有者 model 中,在这一行中:

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

The foreign key needs to be attributed to a model only.外键只需要归因于 model。 But in your code, there is a ForeignKey associated with a form.但是在您的代码中,有一个与表单关联的 ForeignKey。 So, code a model here.所以,在这里编码 model。 After this no need to import this:在此之后不需要导入这个:

from user_app.forms import LoginForm

Hence, it will avoid the circular import error you are facing.因此,它将避免您面临的循环导入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM