Django Many to Many Relationship Add Not Working

2019-04-02 20:06发布

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)

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!

EDIT:

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)

EDIT 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.

0条回答
登录 后发表回答