On 28 oct, 14:05, Daniel Strasser <[EMAIL PROTECTED]> wrote:
> > You have a circular reference between students.models and
> > lessons.models. The first want to import the second, which want to
> > import the first, etc... This just can't work.
>
> Thank you very much. I played around but I don't come to a solution. I
> think I'll try again. I just don't understand where this circular
> reference should be (Problem exists also if I remove them from one
> place or another)
Sorry, it appears I jumped to conclusion a bit too fast. Re-reading
the traceback:
1/ students.models imports lessons.models.Lesson
2/ lessons.models.Lesson try to reference students.models.Student -
which is not defined.
IOW : you don't _actually_ have a circular reference, but you would
have one if you tried to solve this NameError by import
students.models in lessons.models.
The solution is to declare the foreign key on Students using a
app_label.model_name string instead of a model object, ie (not
tested...) :
class Lesson(models.Model):
# ...
student = models.ForeignKey('students.Student')
cf
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey
for more on this.
A bit OT, but using a models.ForeignKey here means that a Lesson can
have *at most one* single student. I can't of course tell whether
that's ok for your app, but it sure looks a bit weird to me.
And while we're at it: given the existence of this foreign key, the
Student.lesson_hours method mentioned in your first post should make
use of it, ie:
class Student(models.Model):
# ...
def lesson_hours(self):
return self.lesson_set.count()
HTH
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---