Question

I'm currently developing a django-tastypie web app. I've two django models :

class Student(models.Model):
  name = models.CharField()

class Course(models.Model):
  name = models.CharField()
  student = models.ForeignKey(Student)

And from that, I've two Tastypie resources in two different files. But here comes my problem. I want to be able to filter student from course, and course from student :

from website.api.course import CourseResource

class StudentResource(ModelResource):
  course = fields.ForeignKey(CourseResource, "course")

  class Meta:
    queryset = Student.objects.all()
    resource_name = "student"
    filtering = { "course" : ALL }

and

from website.api.student import StudentResource

class CourseResource(ModelResource):
  student = fields.ForeignKey(StudentResource, "student")

  class Meta:
    queryset = Course.objects.all()
    resource_name = "course"
    filtering = { "student" : ALL }

But of course, I got a circular import issue. How could I solve that ?

Thanks !

Was it helpful?

Solution

You don't need to import the other resource in each module. Try using a string as the argument instead.

class StudentResource(ModelResource):
    course = fields.ForeignKey('website.api.course.CourseResource', "course")

class CourseResource(ModelResource):
    student = fields.ForeignKey('website.api.student.StudentResource', "student")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top