Django多对多关系添加无法正常工作 [英] Django Many to Many Relationship Add Not Working
问题描述
我正在为其中一个模型使用Django的ManyToManyField。
I'm using Django's ManyToManyField for one of my models.
class Requirement(models.Model):
name = models.CharField(max_length=200)
class Course(models.Model):
requirements = models.ManyToManyField(Requirement)
我希望能够为我的课程分配要求,为此,我尝试以下操作:获得课程,课程,即已保存或刚刚保存,然后运行以下命令:
I want to be able to assign requirements for my classes, so to do that, I try the following: I get a class, course, that is already saved or that I have just saved, and I run the following:
c = Course.objects.get(title="STACK 100")
req = Requirement.objects.get(name="XYZ")
c.requirements.add(req)
虽然我通过Django manage.py shell执行此操作时有效,但当我在脚本中以编程方式执行此操作时不起作用。我在此脚本中使用其他模型,并且一切正常。而且我什至知道我在检查两者时都能成功检索当前课程和要求。我不知道是什么问题!
While this works when I do it through the Django manage.py shell, it does not work when I do it programatically in a script. I work with other models in this script and that all works fine. And I even know it successfully retrieves the current course and the requirement as I check both. I can't figure out what the problem is!
编辑:
我的意思是不工作是该课程的要求字段保持为空。例如,如果我执行c.requirements.all(),我将得到一个空列表。但是,如果我通过外壳执行此方法,则将填充列表。该脚本是使用BeautifulSoup爬网网站的爬网程序。我尝试通过以下功能向课程添加要求:
What I mean by not working is that, the requirements field of the course remains empty. For example, if i do c.requirements.all(), I'll get an empty list. However, if I do this approach through the shell, the list will be populated. The script is a crawler that uses BeautifulSoup to crawl a website. I try to add requirements to courses in the following function:
def create_model_object(self, course_dict, req, sem):
semester = Semester.objects.get(season=sem)
#Checks if the course already exists in the database
existing_matches = Course.objects.filter(number=course_dict["number"])
if len(existing_matches) > 0:
existing_course = existing_matches[0]
if sem=="spring":
existing_course.spring = semester
else:
existing_course.fall = semester
existing_course.save()
c = existing_course
#Creates a new Course in the database
else:
if sem == "spring":
new_course = Course(title=course_dict["title"],
spring=semester)
else:
new_course = Course(title=course_dict["title"],
fall=semester)
new_course.save()
c = new_course
curr_req = Requirement.objects.get(name=req)
c.requirements.add(curr_req)
print(c.id)
编辑2:
进入功能后,这就是我发现的内容:
After stepping into the function, this is what I found:
def __get__(self, instance, instance_type=None):
if instance is None:
return self
rel_model = self.related.related_model
manager = self.related_manager_cls(
model=rel_model,
query_field_name=self.related.field.name,
prefetch_cache_name=self.related.field.related_query_name(),
instance=instance,
symmetrical=False,
source_field_name=self.related.field.m2m_reverse_field_name(),
target_field_name=self.related.field.m2m_field_name(),
reverse=True,
through=self.related.field.rel.through,
)
return manager
根据我的调试器,经理是计划者类型(我的项目名称)。课程。无。
And according to my debugger, manager is of type planner(my project name).Course.None.
推荐答案
我认为您需要调用c.save()在c.requirements.add(curr_req)
I think you need to call c.save() after c.requirements.add(curr_req)
这篇关于Django多对多关系添加无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!