Django跨App传递数据无法导入name字段ImportError报错如何解决
错误原因汇总
1. 直接触发ImportError的核心问题
你在question.models.py中写的from accounts.models import name是错误写法:name是UserInformation模型类的字段属性,不是accounts.models模块的顶层导出变量,无法直接导入。
2. accounts.models存在的其他错误
- 模型没有定义
id_code字段,但你在逻辑中直接使用了self.id_code,运行会触发属性不存在报错 __str__方法缩进错误、没有返回值,且你把生成id_code的逻辑错误写在了__str__方法中,同时先调用self.save()再调用父类save会触发无限递归- 你自定义了
id字段为CharField,覆盖了Django默认的自增主键,如果没有特殊需求建议删除该自定义字段,或显式标注primary_key=True - 视图中用到了
form.instance.user = request.user,但模型中没有定义关联系统用户的user外键字段
3. question.models存在的其他错误
QuestionOne、QuestionTwo模型没有定义name字段,但你在__str__方法中直接返回self.name,会触发属性不存在报错- 代码中用到了
choices=point,但没有定义point选项变量,运行会报错 - 没有关联
UserInformation的逻辑,无法自动同步id_code
4. question.forms存在的错误
QuestionTwoForm的Meta中model属性错写为QuestionOne,和实际字段不匹配,表单无法正常使用
修正后代码示例
accounts/models.py
from django.db import models from django.contrib.auth.models import User class UserInformation(models.Model): # 关联系统用户,和你视图中的逻辑对应 user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user_info") name = models.CharField(max_length=250) lastname = models.CharField(max_length=250) phone = models.CharField(max_length=250) birthday = models.DateField() age = models.CharField(max_length=2) # 补充缺失的id_code字段 id_code = models.CharField(max_length=250, unique=True, blank=True) def __str__(self): # 补充return语句,逻辑放在正确的缩进层级 return f"{self.name}_{self.lastname}_{self.birthday.year}" def save(self, *args, **kwargs): # id_code生成逻辑放在save方法中,避免无限递归 if not self.id_code: self.id_code = f"{self.name}_{self.lastname}_{self.birthday.year}" super().save(*args, **kwargs)
question/models.py
from django.db import models # 正确导入UserInformation类,而非直接导入字段 from accounts.models import UserInformation # 补充你自定义的选项point,根据实际需求修改 point = ( ("1", "1分"), ("2", "2分"), ("3", "3分"), ) class QuestionOne(models.Model): # 外键关联用户信息,方便直接获取对应字段 user_info = models.ForeignKey(UserInformation, on_delete=models.CASCADE, related_name="q1_records") question_1a = models.CharField(max_length=250, choices=point) question_2a = models.CharField(max_length=250, choices=point) question_3a = models.CharField(max_length=250, choices=point) id_code = models.CharField(max_length=250) def __str__(self): # 从关联的用户信息对象获取name字段 return f"{self.user_info.name}_question_1" def save(self, *args, **kwargs): # 自动同步id_code,无需手动赋值 if not self.id_code: self.id_code = self.user_info.id_code super().save(*args, **kwargs) class QuestionTwo(models.Model): user_info = models.ForeignKey(UserInformation, on_delete=models.CASCADE, related_name="q2_records") question_1b = models.CharField(max_length=250, choices=point) question_2b = models.CharField(max_length=250, choices=point) question_3b = models.CharField(max_length=250, choices=point) id_code = models.CharField(max_length=250) def __str__(self): return f"{self.user_info.name}_question_2" def save(self, *args, **kwargs): if not self.id_code: self.id_code = self.user_info.id_code super().save(*args, **kwargs)
question/forms.py
from django import forms from .models import QuestionOne, QuestionTwo class QuestionOneForm(forms.ModelForm): class Meta: model = QuestionOne fields = ('question_1a', 'question_2a', 'question_3a') class QuestionTwoForm(forms.ModelForm): class Meta: # 修正model指向错误 model = QuestionTwo fields = ('question_1b', 'question_2b', 'question_3b')
保存答题的视图示例
# 以保存QuestionOne为例,QuestionTwo同理 def submit_question1(request): if request.method == 'POST': form = QuestionOneForm(request.POST) if form.is_valid(): # 绑定当前登录用户对应的UserInformation form.instance.user_info = request.user.user_info form.save() return redirect('home') else: form = QuestionOneForm() return render(request, 'submit_q1.html', {'form': form})
内容的提问来源于stack exchange,提问作者Kome Gognome
相关产品推荐
相关产品推荐

